# this code was generated by grok 3 beta (x.com) from itertools import product def apply_row_perm(matrix, row_idx): # 行 row_idx の順列に基づいて行を並び替える n = len(matrix) perm = matrix[row_idx] # 行 row_idx の順列(1-based) new_matrix = [None] * n for i in range(n): new_matrix[i] = matrix[perm[i] - 1][:] # 行 perm[i] を i 番目に return new_matrix def apply_col_perm(matrix, col_idx): # 列 col_idx の順列に基づいて列を並び替える n = len(matrix) perm = [matrix[i][col_idx] for i in range(n)] # 列 col_idx の順列(1-based) new_matrix = [[0] * n for _ in range(n)] for j in range(n): target_col = perm[j] - 1 # 列 perm[j] を j 番目に移動 for i in range(n): new_matrix[i][j] = matrix[i][target_col] return new_matrix def check_equal(matrix1, matrix2): # 2つの行列が等しいか確認 return all(row1 == row2 for row1, row2 in zip(matrix1, matrix2)) def simulate_operations(A, operations): # 操作を適用して結果の行列を返す current = [row[:] for row in A] for op_type, pos in operations: if op_type == "R": current = apply_row_perm(current, pos - 1) else: # op_type == "C" current = apply_col_perm(current, pos - 1) return current def solve(N, K, A, B): # すべての可能な K 回の操作を列挙 choices = [(t, p) for t in ["R", "C"] for p in range(1, N + 1)] for ops in product(choices, repeat=K): # A に操作を適用して B になるかチェック simulated_B = simulate_operations(A, ops) if check_equal(simulated_B, B): # B になった場合、逆操作を生成(逆順に適用) reverse_ops = [(op_type, pos) for op_type, pos in reversed(ops)] # 逆操作が A に戻すか確認(デバッグ用) simulated_A = simulate_operations(B, reverse_ops) if check_equal(simulated_A, A): return reverse_ops return [] # 解が見つからない場合(問題保証により発生しない) # 入力処理 N, K = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] B = [list(map(int, input().split())) for _ in range(N)] # 解を求める ops = solve(N, K, A, B) # 出力 print(len(ops)) for op_type, pos in ops: print(f"{op_type} {pos}")