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()) s,g:tuple[x,y: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.dequeue 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<0 or ny<0 or nx>=w or ny>=h:continue if nx==g.x and ny==g.y : quit $(f+1),0 let nk=if M[ny][nx]=='R':1-k else:k if DP[nx][ny][nk]<=f+1:continue DP[nx][ny][nk]=f+1 q.enqueue (nx,ny,nk,f+1) echo -1