ll n,a[5][10][10],b[10][10],h[4][3]; bool f(ll z){ if(z==0){ rep(y,n){ rep(x,n){ if(a[0][y][x]!=b[y][x]){ return false; } } } return true; } --z; rep(p,n){ rep(y,n){ rep(x,n){ a[z][a[z+1][p][y]][x]=a[z+1][y][x]; } } if(f(z)){ h[z][0]=1; h[z][1]=p; return true; } } rep(p,n){ rep(y,n){ rep(x,n){ a[z][y][a[z+1][x][p]]=a[z+1][y][x]; } } if(f(z)){ h[z][0]=0; h[z][1]=p; return true; } } return false; } { ll k; rd(n,k,(a[k]--)(n,n),(b--)(n,n)); f(k); ll m=0; IntMap c(n); rrep(z,k){ if(h[z][0]){ rep(x,n){ c[x]=a[z+1][h[z][1]][x]; } }else{ rep(y,n){ c[y]=a[z+1][y][h[z][1]]; } } ll n=c.calcCycle(); m+=h[z][2]=lcm[i,0,n](c.cycleLen[i])-1; } wt(m); rrep(z,k){ ll p=h[z][1]; ll q[1000]; rep(i,h[z][2]){ if(h[z][0]){ p=a[z+1][h[z][2]][p]; }else{ p=a[z+1][p][h[z][2]]; } q[i]=p; } rrep(i,h[z][2]){ wt(h[z][0]?'R':'C',q[i]+1); } } }