eval"N,K,*A="+`dd`.split*?, y=N*K;a=A[x=0] A.map{x,y,a=[x+K,y+K,(z=_1-a)>0?x+z:x].min,[x+K,y+K,z<0?y-z:y].min,_1} p x