Time limit: 1 second Memory limit: 256 MB Husam, the guard of a building n n is the…
- User
Time limit: 1 second Memory limit: 256 MB Husam, the guard of a building n n is the floor. The floors of this building with numbers 0 0 to n
1 are numbered n−1. This building also has an elevator. This elevator has no limit for the number of people. The key of this elevator is in Hasam's hand and no one can use this elevator until he enters.
Until Hassam reaches the building m m people are waiting for his visitation, people i I want from the floor s i s i to the floor f i f i to go
Hossam also entered the building and saw that the elevator was on the second floor 0 0 and he wanted to go to the floor n
1 go n−1. He wants to be in the way of all this m to bring m people to the destination so that the total movement of the elevator between the floors is minimized.
Please note that Hossam may take a person on one floor and after a lot of delay, take him to the desired floor, and that person's delay is not important at all for Hossam, and only the minimal movement of the elevator is important.
The time the elevator stops on the floors is not important and only the movement of the elevator is important.
input In the first line of the input, two integers and positive numbers n n and m m, which are separated by a space, which shows the number of floors of the building and the number of waiting people, respectively. 2 ≤ n ≤ 1 0 9 2≤n≤10 9
1 ≤ m ≤ 300 , 000 1≤m≤300,000
in m m Next line, in each line, two integers s i s i and f i f i It is stated that the floor number of the person's origin and destination i It shows i. 0 ≤ s i ≠ f i ≤ n
1 0≤s i
=f i ≤n−1
output In the only output line, specify the number of elevator movements between floors.
Example Sample entry 1 10 3 29 5 1 7 6 Plain text Sample output 1 19 Plain text Sample entry 2 7 1 6 0 Plain text Sample output 2 18
- Assistant