#include using namespace std; typedef long long ll; typedef pair P; #define REP(i,n) for(ll i=0;i> f(int r,vector> x){ int n=x[1].size(); vector> v(n, vector(n)); for(int j=1;j> g(int c,vector> x){ int n=x[1].size(); vector> v(n, vector(n)); for(int i=1;i> N >> K; vector> a(N+1, vector(N+1)),b(N+1, vector(N+1)); for(i=1;i<=N;i++) for(j=1;j<=N;j++) cin >> a[i][j]; for(i=1;i<=N;i++) for(j=1;j<=N;j++) cin >> b[i][j]; int p=-1,q=-1,r=-1,s=-1; bool h=false; vector> c,x,y,z,u; for(i=1;i<=2*N;i++){ for(j=1;j<=2*N;j++){ for(k=1;k<=2*N;k++){ for(l=1;l<=2*N;l++){ c=a; if(i<=N) c=f(i,c); else c=g(i-N,c); x=c; if(j<=N) c=f(j,c); else c=g(j-N,c); y=c; if(k<=N) c=f(k,c); else c=g(k-N,c); z=c; if(l<=N) c=f(l,c); else c=g(l-N,c); u=c; if(c==b){ h=true; p=i; q=j; r=k; s=l; break; } } if(h) break; } if(h) break; } if(h) break; } vector v0; vector v2; if(s<=N){ while(u!=z){ int pp=-1; for(i=1;i<=N;i++){ if(u[i][1]==z[s][1]){ pp=i; break; } } u=f(pp,u); v2.push_back(pp); v0.push_back('R'); } }else{ while(u!=z){ int pp=-1; for(i=1;i<=N;i++){ if(u[1][i]==z[1][s-N]){ pp=i; break; } } u=g(pp,u); v2.push_back(pp); v0.push_back('C'); } } if(r<=N){ while(z!=y){ int pp=-1; for(i=1;i<=N;i++){ if(z[i][1]==y[r][1]){ pp=i; break; } } z=f(pp,z); v2.push_back(pp); v0.push_back('R'); } }else{ while(z!=y){ int pp=-1; for(i=1;i<=N;i++){ if(z[1][i]==y[1][r-N]){ pp=i; break; } } z=g(pp,z); v2.push_back(pp); v0.push_back('C'); } } if(q<=N){ while(y!=x){ int pp=-1; for(i=1;i<=N;i++){ if(y[i][1]==x[q][1]){ pp=i; break; } } y=f(pp,y); v2.push_back(pp); v0.push_back('R'); } }else{ while(y!=x){ int pp=-1; for(i=1;i<=N;i++){ if(y[1][i]==x[1][q-N]){ pp=i; break; } } y=g(pp,y); v2.push_back(pp); v0.push_back('C'); } } if(p<=N){ while(x!=a){ int pp=-1; for(i=1;i<=N;i++){ if(x[i][1]==a[p][1]){ pp=i; break; } } x=f(pp,x); v2.push_back(pp); v0.push_back('R'); } }else{ while(x!=a){ int pp=-1; for(i=1;i<=N;i++){ if(x[1][i]==a[1][p-N]){ pp=i; break; } } x=g(pp,x); v2.push_back(pp); v0.push_back('C'); } } int w=v2.size(); cout << w << endl; for(i=0;i