#include using namespace std; using ll = long long; using T = array,10>; int n; void dump(T B){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cerr << B[i][j] + 1 << (j + 1 == n ? '\n' : ' '); } } } T gen(T &A, char c, int p){ T B{{}}; array tr{}; if(c == 'R'){ for(int i = 0; i < n; i++) tr[i] = A[p][i]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ B[tr[i]][j] = A[i][j]; } } }else{ for(int i = 0; i < n; i++) tr[i] = A[i][p]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ B[i][tr[j]] = A[i][j]; } } } return B; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int k; cin >> n >> k; T A{{}}, B{{}}; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> A[i][j]; A[i][j]--; } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> B[i][j]; B[i][j]--; } } vector> dir; for(int i = 0; i < n; i++){ dir.emplace_back('R', i); dir.emplace_back('C', i); } sort(dir.begin(), dir.end()); vector stk; vector> ope; auto dfs = [&](auto dfs, int i) -> bool { if (stk.back() == B) return true; if(i == k) return false; for(auto [c, p] : dir){ ope.emplace_back(c, c == 'C' ? stk.back()[0][p] : stk.back()[p][0]); stk.emplace_back(gen(stk.back(), c, p)); if(dfs(dfs, i + 1)) return true; ope.pop_back(); stk.pop_back(); } return false; }; auto search = [&](char c, int v, T& tmp){ if(c == 'C'){ for(int j = 0; j < n; j++){ if(tmp[0][j] == v) return j; } } for(int j = 0; j < n; j++){ if(tmp[j][0] == v) return j; } return -1; }; stk.emplace_back(A); dfs(dfs, 0); reverse(stk.begin(), stk.end()); reverse(ope.begin(), ope.end()); vector> ans; for(int i = 0; i + 1 < stk.size(); i++){ auto [c, v] = ope[i]; T S = stk[i]; int cnt = -1; for(int k = 1; k <= 250; k++){ S = gen(S, c, search(c, v, S)); if(S == stk[i + 1]){ cnt = k; break; } } if(cnt != -1){ S = stk[i]; while(cnt--){ int p = search(c, v, S); ans.emplace_back(c, p); S = gen(S, c, p); } } } cout << ans.size() << '\n'; for(auto [c, p] : ans) cout << c << ' ' << p + 1 << '\n'; }