QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#16158. Genetic Code

Statistics

The genetic code of an abstract primitivus (Primitivus cycle) is a sequence of natural numbers $K = (A_1, \dots, A_n)$. We define a feature of a primitivus as a pair of numbers $(l, r)$ that appear consecutively in the genetic code, meaning there exists an $i$ such that $l = A_i$ and $r = A_{i+1}$. There are no $(p, p)$ features in the genetic code of a primitivus.

Task

Write a program that: 1. Reads the list of features. 2. Calculates the length of the shortest genetic code containing all given features. 3. Writes the result to the output.

Input

The first line contains a positive integer $n$, which is the number of distinct features of the primitivus. Each of the following $n$ lines contains two space-separated integers $l$ and $r$ ($1 \le l \le 1000, 1 \le r \le 1000$). The pair $(l, r)$ is a feature of the primitivus. Features in the input are not repeated.

Output

Write a single integer on the first line, which is the length of the shortest genetic code for the primitivus.

Examples

Input 1

12
2 3
3 9
9 6
8 5
5 7
7 6
4 5
5 1
1 4
4 2
2 8
8 6

Output 1

15

Note

The following genetic code contains all features from the input: $(8, 5, 1, 4, 2, 3, 9, 6, 4, 5, 7, 6, 2, 8, 6)$

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.