function [label, labels] = find_val(x,labels)
   y = x;
   %fprintf('labels: x: %d, y: %d\n', x, y);
   
   %fprintf(' first\n');
   while (labels(y) ~= y)
       y = labels(y);
   end
   
   %fprintf(' second\n');
   while (labels(x) ~= x)
       z = labels(x);
       labels(x) = y;
       x = z;
   end
   
   label = y;  
end