<>; map{ ($x,$a)=glob; $c=$x?6+int$a/sqrt$x:$a+1; print+(sort{$a-$b}map--$c>0?$c*(($k=int$a/$c)**2+$x)+$a%$c*($k*2+1):1e9,0..39)[0],$/; }<>