#CODEFESTIVAL2016EXA. Distance Pairs

Distance Pairs

Score : 15001500 points

Problem Statement

There is an undirected connected graph with NN vertices numbered 11 through NN. The lengths of all edges in this graph are 11. It is known that for each i(1iN)i (1 \leq i \leq N), the distance between vertex 11 and vertex ii is AiA_i, and the distance between vertex 22 and vertex ii is BiB_i. Determine whether there exists such a graph. If it exists, find the minimum possible number of edges in it.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 0Ai,BiN10 \leq A_i,B_i \leq N-1

Input

The input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

A2A_2 B2B_2

:

ANA_N BNB_N

Output

If there exists a graph satisfying the conditions, print the minimum possible number of edges in such a graph. Otherwise, print -1.

4
0 1
1 0
1 1
2 1
4

The figure below shows two possible graphs. The graph on the right has fewer edges.

3
0 1
1 0
2 2
-1

Such a graph does not exist.