MELE3 - MELE3

Solitaire has N elevators. Each elevator are connecting exactly two floors and it does not stop on the floors between that two floors The speed of all the elevators are the same, 5 seconds to pass one floor.

On the beginning, each elevator is in its lower position and they are starting cruising to the upper floor. After some elevator come to its upper position, it immediately starts to go back to its lower position, and so on...

Mirko is on the first (the lowest) floor and he wants as quick as possible come to the top of the solitaire. He can change elevators only on the floors that are common to both elevators, and if the other elevator is in that moment on that floor, that change does not take any time.

Write a program that will calculate minimal time in which Mirko can get to the top of the solitaire.

Input

>

In the first line of the input file there are two integers K and N, separated with space, number of floors in solitaire and number of elevators, 2 ≤ K ≤ 1000, 1 ≤ N ≤ 50000.

In each of the next N lines there are description of one elevator, two integers A and B, separated with space, 1 ≤ A < B ≤ K, means that elevator is travelling between floors A and B.

There are no two different elevators that travels between same floors.

Note: input data will guarantee that solution will always exists.

Output

In the only line of output file write minimal time (in seconds) from the text above.

Sample

Input:
10 4
1 5
5 10
5 7
7 10

Output:
45
Input:
10 3
1 5
3 5
3 10

Output:
105
Input:
20 5
1 7
7 20
4 7
4 10
10 20

Output:
150

Được gửi lên bởi:psetter
Ngày:2009-04-08
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:COI 03

hide comments
2016-05-08 17:14:50 Nguyen Cuong
6 hit mới AC ._. bị sai công thức mà cứ tưởng là thang máy nào đến sớm nhất thì nhảy lên ._.
2014-12-31 14:48:40 [$Zeus$]
Dijkstra Heap.
2014-11-22 08:36:21 Tây Cuồng
Bài này hay quá :))
2014-11-22 01:55:04 John and the cows
thang máy di chuyển liên tục
2014-07-02 05:02:11 _sanghk11_
bài này dùng dijkstra được ko moi ng............



Last edit: 2014-07-02 05:19:57
2014-01-27 12:46:22  
Em không hiểu đề, ai giải thích giúp với...
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.