#D7635. Floating Islands

    ID: 6344 Type: Default 8000ms 536MiB

Floating Islands

Floating Islands

Problem Statement

You have just arrived in a small country. Unfortunately a huge hurricane swept across the country a few days ago.

The country is made up of nn islands, numbered 11 through nn. Many bridges connected the islands, but all the bridges were washed away by a flood. People in the islands need new bridges to travel among the islands again.

The problem is cost. The country is not very wealthy. The government has to keep spending down. They asked you, a great programmer, to calculate the minimum cost to rebuild bridges. Write a program to calculate it!

Each bridge connects two islands bidirectionally. Each island ii has two parameters did_i and pip_i. An island ii can have at most did_i bridges connected. The cost to build a bridge between an island ii and another island jj is calculated by pipj|p_i - p_j|. Note that it may be impossible to rebuild new bridges within given limitations although people need to travel between any pair of islands over (a sequence of) bridges.

Input

The input is a sequence of datasets. The number of datasets is less than or equal to 6060. Each dataset is formatted as follows.

nn p1p_1 d1d_1 p2p_2 d2d_2 : : pnp_n dnd_n

Everything in the input is an integer. nn (2n4,0002 \leq n \leq 4{,}000) on the first line indicates the number of islands. Then nn lines follow, which contain the parameters of the islands. pip_i (1pi1091 \leq p_i \leq 10^9) and did_i (1din1 \leq d_i \leq n) denote the parameters of the island ii.

The end of the input is indicated by a line with a single zero.

Output

For each dataset, output the minimum cost in a line if it is possible to rebuild bridges within given limitations in the dataset. Otherwise, output 1-1 in a line.

Sample Input

4 1 1 8 2 9 1 14 2 4 181 4 815 4 634 4 370 4 4 52 1 40 1 81 2 73 1 10 330 1 665 3 260 1 287 2 196 3 243 1 815 1 287 3 330 1 473 4 0

Output for the Sample Input

18 634 -1 916

Example

Input

4 1 1 8 2 9 1 14 2 4 181 4 815 4 634 4 370 4 4 52 1 40 1 81 2 73 1 10 330 1 665 3 260 1 287 2 196 3 243 1 815 1 287 3 330 1 473 4 0

Output

18 634 -1 916

inputFormat

Input

The input is a sequence of datasets. The number of datasets is less than or equal to 6060. Each dataset is formatted as follows.

nn p1p_1 d1d_1 p2p_2 d2d_2 : : pnp_n dnd_n

Everything in the input is an integer. nn (2n4,0002 \leq n \leq 4{,}000) on the first line indicates the number of islands. Then nn lines follow, which contain the parameters of the islands. pip_i (1pi1091 \leq p_i \leq 10^9) and did_i (1din1 \leq d_i \leq n) denote the parameters of the island ii.

The end of the input is indicated by a line with a single zero.

outputFormat

Output

For each dataset, output the minimum cost in a line if it is possible to rebuild bridges within given limitations in the dataset. Otherwise, output 1-1 in a line.

Sample Input

4 1 1 8 2 9 1 14 2 4 181 4 815 4 634 4 370 4 4 52 1 40 1 81 2 73 1 10 330 1 665 3 260 1 287 2 196 3 243 1 815 1 287 3 330 1 473 4 0

Output for the Sample Input

18 634 -1 916

Example

Input

4 1 1 8 2 9 1 14 2 4 181 4 815 4 634 4 370 4 4 52 1 40 1 81 2 73 1 10 330 1 665 3 260 1 287 2 196 3 243 1 815 1 287 3 330 1 473 4 0

Output

18 634 -1 916

样例

4
1 1
8 2
9 1
14 2
4
181 4
815 4
634 4
370 4
4
52 1
40 1
81 2
73 1
10
330 1
665 3
260 1
287 2
196 3
243 1
815 1
287 3
330 1
473 4
0
18

634 -1 916

</p>