sub f{@b[$x-1,$x]=@b[$x,$x-1]}@b=0..<>;f$x=<>for 1..<>;@a=glob<>;f,$\.=" $x ".++$x,++$%while($x)=grep$a[$b[$_-1]]>$a[$b[$_]],1..$#a;print$%