COI '10 #3 Rijeka
View as PDFIn the far away land, there is a big river and a number of villages beside it. Villages are numbered 
through 
, in order along the river. The distance between consecutive villages is exactly 
 mile.
Mirko lives in the village marked with . He is in the business of transporting people between villages
with his boat. Today, Mirko will travel from his village to village 
 and he will also transport some
people along the way.
There are  people that wish to travel today, and for each of them, we know their starting point as well
as their destination. Mirko's boat can accommodate as many people as needed.
Let's say for example that person  is traveling from village 
 to village 
, and 
 from village 
 to
village 
. Mirko will, as always, start from his village 
, pick up 
 at village 
, pickup 
 at 
, go back to 
and leave 
 there, proceed to village 
, leave 
 there, and then continue to his final destination, village
. This scenario is given in the first test case below.
Write a program that will find the minimum number of miles that Mirko must travel in order to
transport everyone to their destinations and reach village  at the end.
Input Specification
The first line of input contains integers  and 
 
.
The following  lines contain two integers, starting point and destination for each villager that wants
to travel today. These numbers will be in the range 
 to 
.
Output Specification
Output the minimum number of miles that Mirko must travel.
Scoring
In test cases worth a total of  points, 
 will hold.
In test cases worth a total of  points, 
 will hold.
Sample Input 1
2 10
2 8
6 4
Sample Output 1
14
Sample Input 2
8 15
1 12
3 1
3 9
4 2
7 13
12 11
14 11
14 13
Sample Output 2
27
Comments