Niebieskie ksi.eczki
II Olympiad in Informatics 1994/1995

Task: OPT
Author: Tomasz Śmigielski, Piotr Krysiuk
Disk Optimization

I stage contest  

A disk consists of N sectors numbered successively from 1 to N. Any nonempty sequence of sectors having consecutive numbers is called a block. A length of a block is the number of sectors in the block. Blocks are separate if they have no common sector.

There are files written on a disk. A single file may be written on many sectors, which need not form a single block. To reconstruct a file correctly one needs to read those sectors in right order. This sequence of sectors determines a sequence of separate blocks - each of them contains one or more sectors. Sectors inside each block are read in order of their numbers.

A description of a file distribution on a disk is a sequence of pairs of positive integers. Each of these pairs comprises the number of the starting sector of a block and the length of the block.

Example

The sequence of pairs:
7 3
2 1
5 2
informs that the contents of the file ought to be read successively from the sectors numbered: 7, 8, 9, next: 2, and next: 5, 6.

Each sector on a disk may be either free or there may be written a part of only one file (or one whole file).

Each file has a unique ID - a positive integer in the range of 1 to P, where P is the number of files on the disk.

A disk is optimized when:

One may perform the following operations on a disk:

Copying a block of length t lasts t microseconds. Swapping contents of two blocks of length t lasts 2*t microseconds.

The instruction to copy a block has the form:
K start new_start length
The instruction to swap blocks has the form:
Z start1 start2 length
K and Z are short forms of "kopiowanie" ("copying") and "zamiana" ("swapping"), and start is the number of the first sector of a block.

Task

Write a program that:

Input

In the first line of the data file OPT.IN there are written two positive integers: the number of sectors on the disk N, not greater than 10000, and the number of files P.

Next in the successive lines there are descriptions of file distributions on the disk.

Each file description contains two numbers in the first line: the file's ID from the range of 1 to P and a positive number denoting the number of separate blocks the file is written in. In the next lines there is the description of the file distribution in a form of an appropriate sequence of pairs of positive integers, one pair per line.

The numbers in lines are separated by single spaces.

The data in the file OPT.IN are written correctly and your program need not verify that.

Output

The file OPT.OUT is to contain:

Example

For the file OPT.IN:
200 2
2 2
51 10
41 10
1 2
71 20
11 20
the file OPT.OUT may have the form:
K 21 31 10
K 11 21 10
K 71 1 20
Z 41 51 10

Your program should look for the file OPT.IN in the current directory and create the file OPT.OUT also in the current directory. The source file containing the program written by you should be named OPT.???, where ??? are substituted by at most three-letter abbreviation of the programming language used. The same program in an executable form should be written in a file OPT.EXE.