Shifty Grid

Apply a fixed two-phase procedure of cyclic row and column shifts to sort a permutation grid into row-major order, following the exact TURN steps given.

Hard9SimulationImplementationArrayCombinatoricsNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a rectangular grid of numbered tiles with no empty cell. The grid has NN rows and MM columns, so it holds NMNM tiles, and the tiles carry distinct integers from 00 to NM1NM-1.

The only way to change the grid is a shift operation. One shift moves a whole row left or right by some number of cells, or a whole column up or down by some number of cells. A tile pushed past a border comes back in on the opposite side. For example, in the grid

0   1   2   3
4   5   6   7
8   9   10  11
12  13  14  15

shifting the second column down by one gives

0   13  2   3
4   1   6   7
8   5   10  11
12  9   14  15

A left shift by KK cells has the same effect as a right shift by MKM-K cells, and an upward shift by KK cells has the same effect as a downward shift by NKN-K cells, so the answer uses right shifts and down shifts only.

The grid is solved when the first row holds 00 to M1M-1 in order, the second row holds MM to 2M12M-1 in order, and so on, with the last row holding (N1)M(N-1)M to NM1NM-1 in order. Return the scrambled grid to the solved state.

Input

The first line contains two integers NN and MM (2N,M1002 \le N, M \le 100). Both NN and MM are even. Each of the next NN lines contains MM integers, one row of the grid from left to right. The NMNM tiles are a permutation of 00 to NM1NM-1.

Output

Rows are numbered 11 to NN from the top and columns 11 to MM from the left. Each move is printed on its own line. The line 1 i r (1iN1 \le i \le N, 0r<M0 \le r < M) shifts row ii right by rr cells, and the line 2 j d (1jM1 \le j \le M, 0d<N0 \le d < N) shifts column jj down by dd cells. The tile that belongs at row ii, column jj is the number (i1)M+(j1)(i-1)M + (j-1).

Many different move sequences solve the same grid, so the answer is pinned to one procedure. Print exactly the moves that the procedure below produces.

Write R(i, r) for the move printed as 1 i r, and C(j, d) for the move printed as 2 j d. For 1aM11 \le a \le M-1 and 1bN11 \le b \le N-1, write TURN(i, j, a, b) for these four moves in this order:

R(i, a)
C(j, b)
R(i, M-a)
C(j, N-b)

TURN(i, j, a, b) rotates three tiles and leaves every other tile where it was: the tile in row ii, column jaj-a moves to row ii, column jj; the tile in row ii, column jj moves to row ibi-b, column jj; and the tile in row ibi-b, column jj moves to row ii, column jaj-a. Row and column numbers wrap here, so row 00 means row NN and column 00 means column MM.

The procedure keeps one working grid. It starts as the input grid, and every emitted move is applied to it at once, so each step reads the updated grid.

Step 1. For i=1,2,,N1i = 1, 2, \dots, N-1, and inside each ii for j=1,2,,Mj = 1, 2, \dots, M, let v=(i1)M+(j1)v = (i-1)M + (j-1) and let row xx, column yy be the cell that holds vv.

  • If x=ix = i and y=jy = j, emit nothing.
  • Otherwise, if x=ix = i, emit TURN(i, j, (j-y) mod M, N-1).
  • Otherwise x>ix > i holds. Let dd be (j-y) mod M. If d>0d > 0, emit R(x, d). Then emit TURN(x, j, M-1, x-i).

Step 2. The last row of the working grid now holds the numbers (N1)M(N-1)M to NM1NM-1 in some order. Let tct_c be the tile in row NN, column cc, and let pp be the permutation of 1,2,,M1, 2, \dots, M given by p(c)=tc(N1)M+1p(c) = t_c - (N-1)M + 1. If pp is an odd permutation, emit R(N, 1).

Step 3. For j=1,2,,Mj = 1, 2, \dots, M, let v=(N1)M+(j1)v = (N-1)M + (j-1) and let yy be the column of row NN that holds vv. If y=jy = j, emit nothing. Otherwise let tt be the smallest column such that tjt \ne j, tyt \ne y, and the tile in row NN, column tt is not (N1)M+(t1)(N-1)M + (t-1). Emit these moves in this order: C(t, 1), then R(1, (j-t) mod M), then TURN(N, j, (j-y) mod M, N-1), then R(1, (t-j) mod M), then C(t, N-1).

Let KK be the number of emitted moves. Print KK on the first line, then the KK moves, one per line, in the order they were emitted. The procedure always reaches the solved state and never emits more than 10510^5 moves. When the given grid is already solved, KK is 00 and the output is the single line 0.