PivotOJ

Exhausting Errands

시간 제한: 1000ms메모리 제한: 512MB출처: GCPC 2020BOJ 20905

문제

Dolly the delivery drone is out for a busy working day. It has to complete nn errands in a street where \ell houses are lined up in a row, numbered in ascending order as 1,,1, \dots, \ell. The distance between adjacent houses is 11. Each errand consists of picking up a package at some house aa and delivering it to another house bb. 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 21942 \rightarrow 1 \rightarrow 9 \rightarrow 4 with length 1414.

입력

The input consists of:

  • One line with two integers \ell and nn (11091 \le \ell \le 10^9, 1n1051 \le n \le 10^5), where \ell is the number of houses in the street, and nn is the number of errands.
  • nn lines, each with two integers aa and bb (1a,b1 \le a, b \le \ell with aba \not= b) describing an errand where a package must be picked up at house aa and be delivered to house bb.

출력

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
출력
14

예제 2

입력
100 3
11 50
50 49
36 35
출력
42
코드를 제출하려면 로그인하세요.