You start at the top-left cell $(1, 1)$ of an $n \times n$ grid whose rows and columns are numbered $1$ through $n$ ($1 \le n \le 20000$). For each row $i$ you are given two integers $L(i)$ and $R(i)$ with $1 \le L(i) \le R(i) \le n$, describing a horizontal segment on that row.
Your path must, on every row $i$, visit all cells of that row's segment:
$$(i, L(i)),\ (i, L(i)+1),\ \dots,\ (i, R(i)).$$
You may only move left, right, or down; you can never move up. Moving to an adjacent cell in the same row costs one step, and dropping straight down from row $i$ to row $i+1$ also costs one step. Because you can never move up, the rows must be covered in the order $1, 2, \dots, n$.
After covering the segment on the last row $n$, move to the bottom-right cell $(n, n)$ if you are not already there. Report the total number of steps of the shortest path from $(1, 1)$ to $(n, n)$ that covers every segment.
The first line contains the integer $n$, the number of rows and columns of the grid. Each of the next $n$ lines contains two integers $L(i)$ and $R(i)$ ($1 \le L(i) \le R(i) \le n$), the endpoints of the segment on row $i$.
Print a single integer: the length (number of steps) of the shortest path from $(1, 1)$ to $(n, n)$ that covers every segment $[L(i), R(i)]$.