#include using namespace std; using ll = long long; using T = array,10>; template struct modint61 { using mint = modint61; long long v; modint61() : v(0) {} template modint61(T x) { while(x < 0) x += MOD; while(x >= MOD) x -= MOD; v = x; } long long safe_mod(long long x){ x = (x >> 61) + (x & MOD); if (x >= MOD) x -= MOD; return x; } static constexpr long long mod() { return MOD; } mint& operator++() {v++; if(v == MOD)v = 0; return *this;} mint& operator--() {if(v == 0)v = MOD; v--; return *this;} mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { v += rhs.v; if(v >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; } mint& operator*=(const mint& rhs) { long long u = v >> 31, d = v & MASK31; long long ru = rhs.v >> 31, rd = rhs.v & MASK31; long long mid = d * ru + u * rd; v = safe_mod(u * ru * 2 + (mid >> 30) + ((mid & MASK30) << 31) + d * rd); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint r = 1, x = *this; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { assert(v); return pow(MOD - 2); } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator<(const mint& lhs, const mint& rhs) { return lhs.v < rhs.v; } friend bool operator==(const mint& lhs, const mint& rhs) { return (lhs.v == rhs.v); } friend bool operator!=(const mint& lhs, const mint& rhs) { return (lhs.v != rhs.v); } friend std::istream& operator >> (std::istream& is, mint& rhs) noexcept { long long tmp; rhs = mint{(is >> tmp, tmp)}; return is; } friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; } }; using mint61 = modint61<2305843009213693951>; int n; ll fh(T &A){ mint61 res; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ res *= 1145141919810ll; res += A[i][j]; } } return res.v; } T gen(T &A, char c, int p){ T B{{}}; array tr{}; if(c == 'R'){ for(int i = 0; i < n; i++) tr[A[p][i]] = 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[A[i][p]] = i; 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]--; } } queue que; unordered_map mp; unordered_map> par; ll st = fh(B), gl = fh(A); mp[st] = 0; que.emplace(B); while(!que.empty()){ T C = que.front(); que.pop(); ll hs = fh(C); if(hs == gl) break; int d = mp[hs]; for(int i = 0; i < n; i++){ T D = gen(C, 'R', i); ll hsD = fh(D); if(mp.find(hsD) == mp.end()){ mp[hsD] = d + 1; par[hsD] = make_tuple(hs, 'R', i + 1); if(d <= 999) que.emplace(D); } D = gen(C, 'C', i); hsD = fh(D); if(mp.find(hsD) == mp.end()){ mp[hsD] = d + 1; par[hsD] = make_tuple(hs, 'C', i + 1); if(d <= 999) que.emplace(D); } } } vector> ans; while(gl != st){ auto [fr, ch, ps] = par[gl]; ans.emplace_back(ch, ps); gl = fr; } reverse(ans.begin(), ans.end()); cout << ans.size() << '\n'; for(auto [c, p] : ans) cout << c << ' ' << p << '\n'; }