import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.HashMap; import java.util.Queue; public class Main { public static void main(String[] args) throws NumberFormatException, IOException {Main solve = new Main();solve.solve();} void dump(int[]a){for(int i=0;i0){if((n&1)==1)r*=a;a*=a;n>>=1;}return r;} String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');} void sort(int[]a){m_sort(a,0,a.length,new int[a.length]);} void m_sort(int[]a, int s, int sz, int[]b){ if(sz<7){for(int i=s;is&&a[j-1]>a[j];j--)swap(a, j, j-1);return;} m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int idx=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz; while(la[r])b[idx++]=a[r++];else b[idx++]=a[l++];} while(rv)r=m-1;else{l=m+1;s=m;}}return a[s]>v?-1:s;} void solve() throws NumberFormatException, IOException{ final ContestScanner in = new ContestScanner(); Writer out = new Writer(); int h = in.nextInt(); int w = in.nextInt(); char[][] map = new char[h][]; int sy=0, sx=0; int gy=0, gx=0; for(int i=0; i qu = new ArrayDeque<>(); qu.add(new State(sy, sx, 0, 0)); while(!qu.isEmpty()){ State st = qu.poll(); if(st.y==gy && st.x==gx){ System.out.println(st.d); return; } for(int i=0; i=h || nx<0 || nx>=w || used[ny][nx][st.s]) continue; used[ny][nx][st.s] = true; if(map[ny][nx]=='R'){ qu.add(new State(ny, nx, st.s^1, st.d+1)); }else{ qu.add(new State(ny, nx, st.s, st.d+1)); } } } System.out.println(-1); } final int[][] dx = { {-2, -1, 1, 2, -2, -1, 1, 2}, {-1, 1, -1, 1} }; final int[][] dy = { {-1, -2, -2, -1, 1, 2, 2, 1}, {-1, -1, 1, 1} }; } class State{ int y, x, s, d; State(int y, int x, int s, int d){ this.y = y; this.x = x; this.s = s; this.d = d; } } class MultiSet extends HashMap{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} public void sub(T key) {final int v=get(key);if(v==1)remove(key);else put(key,v-1);} } class Timer{ long time; public void set(){time=System.currentTimeMillis();} public long stop(){return time=System.currentTimeMillis()-time;} public void print() {System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");} @Override public String toString(){return"Time: "+time+"ms";} } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException{super(System.out);} } class ContestScanner { private InputStreamReader in;private int c=-2; public ContestScanner()throws IOException {in=new InputStreamReader(System.in);} public ContestScanner(String filename)throws IOException {in=new InputStreamReader(new FileInputStream(filename));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} }