QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 512 MB مجموع النقاط: 100

#16521. Elevator

الإحصائيات

An elevator is a space that allows one to think deeply.

Given two arrays $a$ and $b$ of length $n$. We call a sequence $p$ of length $m$ valid if and only if:

  • $p_1=1$;
  • For all $1\le i< m$, $|p_i-p_{i+1}|=1$;
  • For all $1\le k\le n$, there exists an ordered pair $(i,j)$ such that $1 \le i < j \le m$, $p_i=a_k$, and $p_j=b_k$.

You need to output the minimum length $m$ among all valid sequences $p$.

Input

The first line contains an integer $n$.

The next $n$ lines each contain two integers $a_i, b_i$.

Output

An integer representing the minimum length $m$ of a valid sequence $p$.

Examples

Input 1

2
3 2
2 5

Output 1

7

Note

The minimum length of the sequence $p$ is $7$. One such sequence $p$ is $\{1, 2, 3, 2, 3, 4, 5\}$.

Input 2

4
4 7
10 8
9 11
4 2

Output 2

18

Constraints

For all test cases, $1 \le n \le 5\times10^5$, $1 \le a_i, b_i \le 10^9$, and it is guaranteed that $a_i \neq b_i$.

This problem uses bundled testing.

Subtask Score $n \le$ Special Properties
$1$ $9$ $1$ None
$2$ $9$ $5\times10^5$ $a_i < b_i$ is guaranteed
$3$ $21$ $5\times10^5$ $a_i, b_i$ are generated randomly in $[1, 10^9]$
$4$ $27$ $2000$ None
$5$ $34$ $5\times10^5$ None

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.