There is an N×N table. The numbers from 1 to N^2 are written in row-major order.
Two operations are available on the table.
To move a number X to position (R, C), use the following process.
The figure below shows one way to move the number 6 to (3, 4).

You will move K numbers in order. After moving one number, the table is not reset; the next number is moved from the table's current state. Compute how many rotations are needed for each move.
The first line contains the table size N and the number of values to move, K.
Each of the next K lines contains a number X and a target position R, C.
The numbers must be moved one by one in the order given.
Print K lines. On each line, print the number of rotations needed for the corresponding move.