Problem list
Log in
#usaco0087 · USACO 2012-2013 February Gold

Taxi

Platinum II 2.0 s 256 MB

Bessie is running a taxi service for the other cows on the farm. The cows have been gathering at different locations along a fence of length M (1 <= M <= 1,000,000,000). Unfortunately, they have grown bored with their current locations and each wish to go somewhere else along the fence. Bessie must pick up each of her friends at their starting positions and drive them to their destinations. Bessie's car is small so she can only transport one cow in her car at a time. Cows can enter and exit the car instantaneously.

To save gas, Bessie would like to minimize the amount she has to drive. Given the starting and ending positions of each of the N cows (1 <= N <= 100,000), determine the least amount of driving Bessie has to do. Bessie realizes that to save the most gas she may need to occasionally drop a cow off at a position other than her destination.

Bessie starts at the leftmost point of the fence, position 0, and must finish her journey at the rightmost point on the fence, position M.

Input

  • Line 1: N and M separated by a space.
  • Lines 2..1+N: The (i+1)th line contains two space separated integers, s_i and t_i (0 <= s_i, t_i <= M), indicating the starting position and destination position of the ith cow.

Output

A single integer indicating the total amount of driving Bessie must do. Note that the result may not fit into a 32 bit integer.

Example 1

Input
2 10
0 9
6 5
Output
12

Output details: Bessie picks up cow 1 at 0 and drives to 6, drops cow 1, delivers cow 2 to her destination, returns to pick up cow 1, drops her off, then drives to the right end of the fence.

Source
USACO 2012-2013 February Gold
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code