시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 (추가 시간 없음) 512 MB 0 0 0 0.000%

문제

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.

Figure E.1: Illustration of Sample Input 1. The shortest route is $2 \rightarrow 1 \rightarrow 9 \rightarrow 4$ with length $14$.

입력

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 a single line with the minimal distance Dolly has to cover from picking up the first package until delivering the last package.

예제 입력 1

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

예제 출력 1

14

예제 입력 2

100 3
11 50
50 49
36 35

예제 출력 2

42

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > German Collegiate Programming Contest > GCPC 2020 E번

  • 문제를 만든 사람: Sandro Esquivel