Under construction

Still being built. Things may change

Fireworks in RightAngleles

Time limit1sMemory limit128 MB

Summary
Choose a vertical launch street V minimizing total Manhattan-like walking distance for citizens to reach nearest allowed point on the two crossing streets, respecting a safety gap S.
Level

Hard8 of 10

Topics
Binary search, Greedy, Math
Solved
No attempts yet

Problem

In the city of RightAngleles the streets form an infinite square grid: every pair of streets is either parallel or perpendicular, and neighboring parallel streets are exactly one unit apart. Streets running West-East are called horizontal and are numbered by consecutive integers from South to North; streets running South-North are called vertical and are numbered by consecutive integers from West to East. Each intersection is identified by the number of its horizontal street and the number of its vertical street.

Every citizen lives at the intersection where their house entrance is located, and several citizens may live at the same intersection.

The mayor wants to hold a fireworks display at the intersection of the main horizontal street (numbered 00) and some vertical street VV. The fireworks are visible only along the two streets that meet there — the main horizontal street and vertical street VV. For safety, every spectator must stand at least SS units away from the launch intersection. Concretely, a citizen may watch the display from either:

  • an intersection on the main horizontal street whose vertical street number differs from VV by at least SS, or
  • an intersection on vertical street VV whose horizontal street number is at least SS away from 00.

For example, when S=2S = 2 the display may be watched from any intersection of the main horizontal street except those lying on vertical streets V−1V-1, VV, and V+1V+1, and from any intersection of vertical street VV except those lying on horizontal streets −1-1, 00, and 11.

Citizens move only along the streets, so the distance a citizen walks to reach a watching spot equals the number of unit segments travelled. Each interested citizen walks to a nearest allowed watching spot, and the mayor chooses the vertical street VV so that the total distance walked by all citizens is as small as possible.

Write a program that computes this minimum possible total distance.

Input

The first line contains two positive integers NN and SS separated by a space: the number of citizens NN (N≤105N \le 10^5) and the safety distance SS (S≤106S \le 10^6).

Each of the next NN lines contains two integers HiH_i and ViV_i separated by a space (−109≤Hi,Vi≤109-10^9 \le H_i, V_i \le 10^9): the horizontal street number HiH_i and the vertical street number ViV_i of the intersection where the ii-th citizen lives.

Output

Print a single integer: the minimum total distance (in units) that the citizens must walk in order to watch the fireworks.

Examples5

  1. Example 1

    Input
    7 2
    3 -2
    0 8
    -4 8
    -1 4
    -2 13
    -4 8
    1 5
    
    Expected output
    9
    
  2. Example 2

    Input
    1 1
    0 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1 3
    1 4
    
    Expected output
    1
    
  4. Example 4

    Input
    3 2
    0 0
    0 10
    0 20
    
    Expected output
    0
    
  5. Example 5

    Input
    3 5
    1 0
    1 10
    1 20
    
    Expected output
    3