Hide

Problem E
Exhausting Errands

Dolly the delivery drone is out for a busy working day. It has to complete $n$ errands in a street where $\ell $ houses are lined up in a row, numbered in ascending order as $1, \dots , \ell $. The distance between adjacent houses is $1$. Each errand consists of picking up a package at some house $a$ and delivering it to another house $b$. Dolly can start with any errand, complete the errands in any order, and is able to carry an unlimited number of packages at the same time. Your job is to find the minimal total distance Dolly has to cover to complete all errands. The delivery route can start and finish at arbitrary locations along the street.

\includegraphics[width=0.9\textwidth ]{illustration_dolly}
Figure 1: Illustration of Sample Input 1. The shortest route is $2 \rightarrow 1 \rightarrow 9 \rightarrow 4$ with length $14$.

Input

The input consists of:

  • One line with two integers $\ell $ and $n$ ($1 \le \ell \le 10^9$, $1 \le n \le 10^5$), where $\ell $ is the number of houses in the street, and $n$ is the number of errands.

  • $n$ lines, each with two integers $a$ and $b$ ($1 \le a, b \le \ell $ with $a \not= b$) describing an errand where a package must be picked up at house $a$ and be delivered to house $b$.

Output

Output a single line with the minimal distance Dolly has to cover from picking up the first package until delivering the last package.

Sample Input 1 Sample Output 1
10 6
1 4
3 5
6 7
2 1
9 4
8 5
14
Sample Input 2 Sample Output 2
100 3
11 50
50 49
36 35
42
CPU Time limit 2 seconds
Memory limit 1024 MB
Statistics Show
License Creative Commons License (cc by-sa)

Please log in to submit a solution to this problem

Log in