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 |