This page is still under construction.

Parts of this page are still being built. What you see may change.

Trapped in the Haybales

Time limit1sMemory limit256 MB

Summary
Bessie breaks any bale smaller than her running start, and you must find the cheapest single bale to enlarge so she never leaves the outer bales.
Level

Medium7 of 10

Topics
Two pointers, Sorting, Simulation
Solved
No attempts yet

Problem

Farmer John received NN hay bales and put them at various points along the straight road that connects his barn with his house. Bale jj has size SjS_j and position PjP_j, and no two bales share a position. Bessie the cow stands at position BB, where there is no bale.

Bessie moves freely along the road and can walk right up to the position of a bale, but she cannot pass through that position. There is one exception: if she runs in the same direction for DD units of distance, she builds up enough speed to smash through a bale of size strictly less than DD and remove it for good. Removing a bale gives her a longer run, so she may smash other bales as well.

FJ is repainting his house and his barn, and he wants Bessie to reach neither one. He therefore wants to make sure she never breaks through the leftmost or the rightmost bale. To help, he can pick a single bale and add hay to it, raising its size by any nonnegative amount. Find the smallest amount of hay he has to add to keep Bessie trapped.

Input

The first line contains NN and Bessie's starting position BB. Each of the next NN lines describes one bale with two integers, its size and its position.

1≤N≤100,0001 \le N \le 100{,}000, and every size, every position and BB are integers between 11 and 10910^9. The positions are distinct, and no bale sits at position BB.

Output

Print a single integer, the smallest amount of hay FJ has to add. Print 00 if Bessie is already trapped. Print −1-1 if she escapes no matter which bale gets hay and how much.

Examples7

  1. Example 1

    Input
    5 7
    8 1
    1 4
    3 8
    12 15
    20 20
    
    Expected output
    4
    
  2. Example 2

    Input
    2 5
    10 1
    10 9
    
    Expected output
    0
    
  3. Example 3

    Input
    2 5
    10 1
    3 9
    
    Expected output
    5
    
  4. Example 4

    Input
    4 3
    10 1
    1 5
    1 9
    50 12
    
    Expected output
    1
    
  5. Example 5

    Input
    1 5
    100 3
    
    Expected output
    -1
    
  6. Example 6

    Input
    3 100
    5 1
    5 2
    5 3
    
    Expected output
    -1
    
  7. Example 7

    Input
    4 3
    1 1
    1 2
    1 4
    1 5
    
    Expected output
    -1