cho.sh
Notes
Loading...

Making a Convex Polygon

Time limit

2s

Memory limit

128 MB

Problem

Points numbered 1 through N are placed clockwise on a circle. Each point is connected by segments to two distinct other points, so the given connections form an undirected graph where every point has degree 2.

If some segments cross, the current circular order is not the boundary order of a convex N-gon. One move takes one point and places it at another position on the circle; the connections themselves do not change.

Find the minimum number of point moves needed so that the connected segments no longer cross and become the sides of a convex N-gon. If this cannot be done, output -1.

Input

The first line contains the number of points N (1 <= N <= 500).

For each i = 1, 2, ..., N, one line follows with two integers a and b. This means that point i is connected to points a and b.

Output

If a convex N-gon can be made, output the minimum number of moves required. If it cannot be made, output -1.