Under construction

Still being built. Things may change

Jumbled Stacks

Time limit1sMemory limit1024 MB

Summary
Move top cards between capacity-limited stacks so cards 1 to n end up sorted bottom to top, packed into the lowest-indexed stacks.
Level

Medium6 of 10

Topics
Simulation, Stack, Implementation
Solved
No attempts yet

Problem

We are given a set of nn cards, labelled from 1 to nn, which are distributed into kk stacks S1,S2,…,SkS_1, S_2, \ldots, S_k. Each stack has a limited capacity: the ii-th stack, SiS_i, can contain at most CiC_i cards. The only way to manipulate these cards is to take the top card of a stack and move it to the top of some other stack, as long as this does not exceed the capacity of the destination stack.

Using a sequence of such moves, we would like to rearrange the cards so that the first few stacks (0 or more) with the smallest indices are filled to capacity, the stack immediately after them is not filled to capacity (and may even be empty), and all stacks after that are completely empty. Moreover, if you stack all the stacks from S1S_1 at the bottom to SkS_k at the top, the cards must be ordered from smallest to largest, with 11 at the bottom and nn at the top.

It is guaranteed that n≤(∑i=1kCi)−max⁡1≤i≤kCin \le \left(\sum_{i=1}^k C_i\right) - \max_{1 \le i \le k} C_i.

Suppose we had n=6n = 6 cards on k=3k = 3 stacks, with capacities C1=4C_1 = 4, C2=C3=3C_2 = C_3 = 3, and with the following initial state: S1=[2,3,0,0]S_1 = [2, 3, 0, 0] (from bottom to top; 00 indicates an empty slot), S2=[4,1,6]S_2 = [4, 1, 6], S3=[5,0,0]S_3 = [5, 0, 0]. Then the desired end state is S1=[1,2,3,4]S_1 = [1, 2, 3, 4], S2=[5,6,0]S_2 = [5, 6, 0] and S3=[0,0,0]S_3 = [0, 0, 0].

Input

The first line contains two integers, nn (the number of cards) and kk (the number of stacks), separated by a space. The remaining kk lines describe the initial state of the stacks. The ii-th of these lines describes SiS_i and contains Ci+1C_i + 1 integers, separated by spaces. The first of these integers is CiC_i (the capacity of the stack SiS_i), and the rest are the labels of the cards on SiS_i, from bottom to top. If the stack SiS_i contains fewer than CiC_i cards (it could even be empty), the last few integers in the line are 0.

Output

Print a sequence of moves that brings the stacks into the desired end state. For each move, output a line with two integers separated by a space: first the number of the stack the card is moved from, then the number of the stack it is moved to. The stacks are numbered from 1 to kk, and the destination must differ from the source. The number of moves must not exceed 10510^5. After the last move, print a line containing "0 0" (without the quotation marks). If there are several possible solutions, you may output any of them.

Constraints

  • 1≤n≤1001 \le n \le 100
  • 3≤k≤1003 \le k \le 100
  • 1≤Ci≤n1 \le C_i \le n

Hint

This is the example discussed earlier in the problem statement. The sample output shows a sequence of 14 moves that brings the stacks into the desired end state.

Examples1

  1. Example 1

    Input
    6 3
    4 2 3 0 0
    3 4 1 6
    3 5 0 0
    
    Expected output
    2 3
    2 3
    1 2
    1 2
    3 1
    2 1
    2 1
    3 2
    3 1
    2 3
    1 3
    2 1
    3 2
    3 2
    0 0