function n = leastlab(a,index)
%
% leastlab: find least equivalent label (used by label codes)
% On input:
%     a: 
%     index:
% On output:
%     n : least label
% Call:
%     n = leastlab(a,index)
%

lena = length(a);
n = 0;

if index>0 & index<=lena
  i1 = index;
  while (i1>0) & (i1<=lena) & (i1~=a(i1))
    i1 = a(i1);
  end
  n = i1;
end