sub r{/./?$_=&r:0for$u[$r=$_];$r} sub u{($a,$b)=map r,@_;@u[$b,$a]=$a} <>; $h=3e5; map{ ++$%,$x*2<$l+$h? map{u$x++,$l++}$l..$x-1: map{u--$h+$l-$x,$h}$x..$h-1 if($x=r)-$l; }glob<>; print$%;