import sequtils,strutils var hw=stdin.readLine.split.map parseInt h=hw[0] w=hw[1] M=newSeqWith h:stdin.readLine g,o:int qi=0 q=newSeq[(int,int,int,int)]() D=newSeqWith w:newSeqWith h:[9999,9999] for y in 0..0:(let(x,y,k,f)=q[qi];qi+=1;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(n,m)=(x+d[0],y+d[1]);if n in 0..f+1:(D[n][m][z]=f+1;q&=(n,m,z,f+1))))) echo -1