TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Modern writers do not use pens to write books. They enter the text directly on to the home PCs using MS Word and edit the text when necessary.


After entering the text for a new book, a famous novelist desires to have a major rearrangement of the text he has prepared. He has identified passages in the text and numbered
these passages. However the passages are not in proper equence. He wants to use the minimum number of cut and paste operations of MS Word to edit the text and put the passages
in proper order. He may do the operation either with one passage or with a number of passages occurring in a sequence. He needs a program for this purpose.



The following example illustrates his problem. In order to edit the text containing 6 passages in the sequence P2, P4, P1, P5, P3, P6 so that the passages appear in the text in the
proper sequence viz., P1, P2, P3, P4, P5, P6 he needs at least 2 cut and paste operations:

  1. cut P1 and paste before P2 to get P1, P2, P4, P5, P3, P6
    and 2. cut P3 and paste after P2 to get P1, P2, P3, P4, P5, P6.

    Can you write a program for him?

Input Format

The input may contain multiple test cases.



For each test case, the first line contains two integers, the case number c and the total number n of passages.



For simplicity, passages are represented by integers 1, 2, ??n. The input sequence of passages is given in n lines, each line containing one integer in the order in which the passages
appear in the text before editing.



The entire input set terminates with an input 0 for c.

Output Format

For each test case in the input, print the test case number c and the total number k of cut.



Print a blank line between outputs of two test cases.

Sample Input 1

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

Sample Output 1

1 2

2 4

Hints

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 10000 65536 200