const Mat = Vector{Vector{Int}} function reorderColsByColPerm(s::Matrix, x::Int) t = deepcopy(s) for r = 1:size(t,1), c = 1:size(t,1) s[ r , t[c,x] ] = t[r,c] end end function reorderRowsByRowPerm(s::Matrix, y::Int) t = deepcopy(s) for r = 1:size(t,1), c = 1:size(t,1) s[ t[y,r] , c ] = t[r,c] end end function search(b::Matrix, k::Int, t::Matrix, v::Vector{Int})::Bool if k == 0 return b == t end n = size(t,1) for i = 1:n*2 v[k] = i s = deepcopy(t) if i <= n reorderColsByColPerm(s, i) else reorderRowsByRowPerm(s, i-n) end if search(b, k-1, s, v) return true end end false end function cycle(p::AbstractVector{Int})::Int w = copy(p) t = copy(p) cnt = 0 while true cnt += 1 for i = 1:length(p) t[p[i]] = w[i] end if t == p return cnt end w = copy(t) end end function solve(n::Int, k::Int, a::Matrix, b::Matrix)::Vector{Int} ops = Vector{Int}(undef,k) search(b, k, a, ops) ans = Int[] for op in reverse(ops) p = op <= n ? selectdim(a,2,op) : selectdim(a,1,op-n) z = op <= n ? op : (op-n) cnt = cycle(p) - 1 tmp = Vector{Int}(undef,cnt) for i in 1:cnt z = p[z] tmp[i] = op <= n ? z : (z+n) end ans = vcat(tmp, ans) if op <= n reorderColsByColPerm(a, op) else reorderRowsByRowPerm(a, op-n) end end ans end function main() readInts() = map(s -> parse(Int, s), split(readline(), " ")) toMatrix(m) = reduce(vcat, transpose(m)) n, k = readInts() a = toMatrix([readInts() for _ in 1:n]) b = toMatrix([readInts() for _ in 1:n]) ans = solve(n, k, a, b) println(length(ans)) for op in ans if op <= n println("C $op") else println("R $(op-n)") end end end main()