module Main let readInts () : int array = stdin.ReadLine().Split([|' '|]) |> Array.map int type matrix = int[,] let reorderColsByColPerm (s : matrix) (x : int) : matrix = let t = Array2D.copy s for r = 1 to Array2D.length1 s do for c = 1 to Array2D.length2 s do let v = Array2D.get s (r-1) (c-1) let cx = Array2D.get s (c-1) (x-1) Array2D.set t (r-1) (cx-1) v t let reorderRowsByRowPerm (s : matrix) (y : int) : matrix = let t = Array2D.copy s for r = 1 to Array2D.length1 s do for c = 1 to Array2D.length2 s do let v = Array2D.get s (r-1) (c-1) let ry = Array2D.get s (y-1) (r-1) Array2D.set t (ry-1) (c-1) v t let rec search (b : matrix) (k : int) (t : matrix) (v : int array) : bool = if k = 0 then b = t else let n = Array2D.length1 b seq { 1..n*2 } |> Seq.exists (fun op -> Array.set v (Array.length v - k) op let s = if op <= n then reorderColsByColPerm t op else reorderRowsByRowPerm t (op-n) search b (k-1) s v ) let cycle (p : int array) : int = let rec f (cnt : int) (t : int array) (w : int array) : int = p |> Array.iteri (fun i e -> Array.set t (e-1) (Array.get w i)) if p = t then cnt else f (cnt+1) w t f 1 (Array.copy p) (Array.copy p) let solve (n : int) (k : int) (a : matrix) (b : matrix) : int array = let ops = Array.zeroCreate k ignore <| search b k a ops let f ((ans, s) : int array * matrix) (op : int) : int array * matrix = let p = if op <= n then s[*,op-1] else s[op-1-n,*] let t = if op <= n then reorderColsByColPerm s op else reorderRowsByRowPerm s (op-n) let cnt = cycle p - 1 let z0 = if op <= n then op else (op-n) let tmp = seq { 1..cnt } |> Seq.scan (fun z _ -> Array.get p (z-1)) z0 |> Seq.tail |> Seq.map (fun z -> if op <= n then z else (z+n)) |> Seq.toArray (Array.append tmp ans, t) ops |> Array.fold f ([||],a) |> fst [] let main (_args: string array) : int = let [|n; k|] = readInts() let a = array2D [|for _ in 1..n -> readInts()|] let b = array2D [|for _ in 1..n -> readInts()|] let ans = solve n k a b printfn "%d" (Array.length ans) let f (op : int) : string = if op <= n then (sprintf "C %d" op) else (sprintf "R %d" (op-n)) ans |> Array.map f |> Array.iter (printfn "%s") 0