C is playing a game. There are poles in front of him, numbered from
to
. At the beginning, the poles
to
have
balls, placed from the bottom to the top. The
th pole has no balls on it. The
balls have
colors, with each color having
balls.
C is trying to move the balls so that in the end, every pole only contains the balls of the same color, notice that it does not matter which pole has which color.
C can reach the goal using a number of operations, moving one ball from one pole to the other. More specifically, C can move the ball from pole to pole
if and only if:
- There is at least one ball on pole
.
- There are at most
balls on pole
.
- C can only move the top ball of pole
to the top of pole
.
C's goal is not hard to reach, so he challenges you to reach his goal in at most operations. Print out one way to reach his goal.
Input Specification
The first line contains two integers, denoting the number of colors of the balls and the number of balls of each color.
The next
lines contain
integers each, the
th line denoting the balls on the
th pole.
Output Specification
The first line of output contains one integer that satisfies
, denoting the number of operations.
The next
lines contain two integers
, denoting moving a ball from pole
to pole
.
should satisfy
and
.
Any valid solution will be accepted.
Sample Input
2 3
1 1 2
2 1 2
Sample Output
6
1 3
2 3
2 3
3 1
3 2
3 2
Constraints
For all test cases, ,
.
For partials, refer to the table below.
Test Cases | ||
---|---|---|
1~2 | ||
3~5 | ||
6~8 | ||
9~14 | ||
15~20 |
Comments