# 570 import sequtils,queues,strutils proc R:any=stdin.readLine var hw=R().split.map parseInt h=hw[0] w=hw[1] M=newSeqWith(h,R()) gx,gy:int q=initQueue[tuple[x,y,k,d:int]]() DP=newSeqWith(w,newSeqWith(h,[9999,9999])) for y in 0..0: let(x,y,k,f)=q.pop for d in[@[(1,1),(-1,-1),(1,-1),(-1,1)],@[(1,2),(2,1),(-1,-2),(-2,-1),(-1,2),(-2,1),(1,-2),(2,-1)]][k]:(let(nx,ny)=(x+d[0],y+d[1]);if nx in 0..f+1:(DP[nx][ny][nk]=f+1;q.add (nx,ny,nk,f+1)))) echo -1