In a hotel there are k
lifts, which you determine at the beginning of the day on which floors they are. During the day, n
requests will arrive, each characterized by a pair (l,r)
. This means someone is on the l
-th floor and wants to go to the r
-th floor. The i
-th request must be completed before the i+1
-th. The lifts are small and must be empty or carrying exactly one passenger at any given time. We want to minimize the sum of floors in which the lifts travel empty. If an lift travels from floor p
to floor q
without a passenger, then we consider it to have travelled |p-q|
floors empty. Unfortunately, the hotel is old and the machines only have 64 MB of memory.
Input
The first line of the standard input contains the numbers n
and k
. The next n
lines contain 2
numbers each - (l,r)
for the respective request.
Output
On the single line of the standard output, print the minimum sum of floors where the lifts travel empty
Constraints
1 ≤ n ≤ 10 000
1 ≤ k ≤ min(30,n)
Subtask 1 (5 points)
n ≤ 22
Subtask 2 (20 points)
n ≤ 250
Subtask 3 (10 points)
n ≤ 600
Subtask 4 (15 points)
n ≤ 1250
Subtask 5 (20 points)
n ≤ 2500
Subtask 6 (30 points)
- No other constraints
Sample
stdin
3 2
5 20
8 100
2 80
stdout
12
Explanation
Lift 1
is initially on floor 5
and lift 2
is on floor 8
.
Lift 1
travels 0
floors empty and carries the passenger to the 20
th floor.
Lift 1
travels 12
floors empty and carries the passenger to the 100
th floor.
Lift 2
travels 0
floors empty and carries the passenger to the 80
th floor.
The total number of floors where the lifts travel empty is 0+12+0=12
.