
      INTERFACE
	FUNCTION rank (next)
	INTEGER next (:)
	INTEGER rank (SIZE (next))
        ENDFUNCTION
      ENDINTERFACE

      PRINT *, rank ((/ 4, 5, 6, 3, 5, 2 /))
      ENDPROGRAM


      FUNCTION rank (next)
      INTEGER next (:)
      INTEGER rank (SIZE (next))

        INTEGER, DIMENSION (SIZE (next)) :: p, distance, index, disp

	n = SIZE (next)
        index = (/ (i, i = 1, n) /)
        p = next
        WHERE (p /= index)
          distance = 1
        ELSEWHERE
          distance = 0
        ENDWHERE
        i = 1
        DO WHILE (i < n)
          disp = distance (p)
          WHERE (p /= p (p))
            distance = distance + disp
            !distance = distance + distance (p)
          ENDWHERE
	  p = p (p)
          i = 2 * i
        ENDDO
        rank = distance
      ENDFUNCTION
