QOJ.ac

QOJ

시간 제한: 2 s 메모리 제한: 1024 MB 총점: 100

#23339. Collecting Colors

통계

There are $2N$ balls and $N+1$ boxes. Each ball has a color from $1,2,\ldots,N$, and exactly two balls have each color. Each box can hold at most two balls. Initially, boxes $1$ through $N$ each contain two balls, and box $N+1$ is empty. In box $i$, the top ball has color $A_i$ and the bottom ball has color $B_i$.

You may move a ball from one box to another by taking the top ball out of one box and putting it on top of another box.

You must move balls so that the two balls of every color end up in the same box. Each move must satisfy one of the following two conditions.

  • The destination box is completely empty.
  • The destination box contains exactly one ball, and its color is the same as that of the ball being moved.

Write a program that finds the minimum number of moves needed to put balls of the same color in the same box.

problem_23339_e7fea3f67994332b5cbecccaf677670d.png

For example, the following moves in the figure above achieve this in 6 moves, and fewer than 6 moves cannot achieve the goal.

  • Move the ball of color 3 from box 4 to box 6.
  • Move the ball of color 2 from box 3 to box 4.
  • Move the ball of color 4 from box 1 to box 3.
  • Move the ball of color 3 from box 2 to box 6.
  • Move the ball of color 5 from box 5 to box 2.
  • Move the ball of color 1 from box 5 to box 1.

Constraints

  • All given numbers are integers.
  • $1\le N\le200\,000$.
  • $1\le A_i,B_i\le N$.
  • For every $i$ ($1\le i\le N$), exactly two values among $A_1,A_2,\ldots,A_N,B_1,B_2,\ldots,B_N$ equal $i$.

Subtasks

  1. (2 points) $N\le2$.
  2. (23 points) $N\le20$.
  3. (15 points) There exists a way to move the balls so that balls of the same color are in the same box.
  4. (15 points) $N\le2\,000$.
  5. (45 points) No additional constraints.

Input

The first line contains $N$.

The following $N$ lines give the colors of the two balls in each box. The $i$-th of these lines contains $A_i$ and $B_i$, separated by a space.

Output

On the first line, output the minimum number of moves needed to put balls of the same color in the same box.

If this is impossible, output $-1$.

Examples

Input 1

5
4 1
3 5
2 4
3 2
5 1

Output 1

6

Input 2

2
1 1
2 2

Output 2

0

Input 3

4
2 1
3 1
2 4
3 4

Output 3

-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.