Problem 4: Bug Infestation

Having fortunately gotten wind of the Cow-Bot's construction before meeting it in battle, the monkeys are in the process of frantically creating a virus, with the hopes of installing it into the robot and shutting it down! They've finished writing their program, which consists of N (1 ≤ N ≤ 300,000) lines of code, but unfortunately its quality may be somewhat lacking...

At any point in time, each line of code either contains a bug, or doesn't. Initially, each line i contains a bug if Bi = 1, and otherwise doesn't if Bi = 0 (0 ≤ Bi ≤ 1).

Each minute, the monkeys can select one line of code which contains a bug, and fix that bug! Unfortunately, their code is so fragile that fixing some bugs may introduce others. If a bug is fixed on line i, then if Li = 0, there are no consequences. Otherwise, one other line Li (1 ≤ LiN, Lii) will begin to have a bug (regardless of whether it already had one or not).

In order to efficiently proceed with making their viral code as correct as possible, the monkeys are interested in the answers to two questions. Firstly, what's the minumum possible number of lines of code which can be left containing bugs after they fix as many bugs as they'd like to? And secondly, how quickly can that minimum number of outstanding bugs be achieved? If Q = 1, then you only need to answer the first of these questions, and if Q = 2, then you must answer both.

In test cases worth 5/30 of the points, Q = 1 and N ≤ 2000.
In test cases worth another 4/30 of the points, Q = 1.
In test cases worth another 6/30 of the points, N ≤ 2000.

Input Format

The first line of input consists of two space-separated integers N and Q.
N lines follow, the i-th of which consists of two integers Bi and Li (for i = 1..N).

Output Format

If Q = 1, then output a single integer – the minimum possible number of bugs which can remain in the code.
Otherwise if Q = 2, then output two space-separated integers – the minimum possible number of bugs which can remain in the code, and the minimum number of minutes required to achieve that number of bugs.

```10 2
1 4
0 0
0 8
0 10
0 0
1 0
1 4
1 4
1 5
0 7
```

```1 6
```

Sample Explanation

One optimal sequence of lines to debug is: 1, 6, 7, 8, 9, 5.
After this sequence, the only line containing a bug will be line 4. It's impossible to eliminate all of the bugs from the monkeys' code (hopefully the same isn't true for yours…).

Point Value: 20 (partial)
Time Limit: 3.00s
Memory Limit: 64M