Apocalypse


Submit solution

Points: 12 (partial)
Time limit: 2.0s
Memory limit: 256M

Problem type

In a land long gone and a time long forgotten, two titans clashed in a battle that would define an era. Zephyra, a sorceress who controlled gravity itself, faced Aetherius, a master of agility and grace.

She invoked her ultimate technique — the Apocalypse Attack — calling down meteors from the cosmos. Each meteor struck with crushing gravitational force, making movement difficult.

Aetherius, moving along a one-dimensional number line, must avoid each meteor as it falls. A meteor affects a segment on the number line; if Aetherius is on or beyond the edge of that segment at the moment of impact, he successfully dodges it.

Between impacts, he may move freely to any position — but his goal is to minimize his total movement.

Your task is to determine: What is the minimum total distance Aetherius must travel to dodge all meteors?

Input Specification

The first line contains two integers \(n\) and \(s\) \((1 ≤ n ≤ 10^5, 1 ≤ s ≤ 10^5)\):

  • \(n\) = number of meteors
  • \(s\) = Aetherius's starting position on the number line

Each of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) \((1 ≤ l_i ≤ r_i ≤ 10^5)\), representing the impact range of the \(i\)-th meteor.

Output Specification

Print one integer: the minimum distance Aetherius must move to dodge all meteors.

Sample Input 1

1 5
1 7

Sample Output 1

2

Sample Input 2

3 7
6 12
1 9
8 14

Sample Output 2

5

Comments


  • 0
    Deo  commented on Nov. 17, 2025, 8:49 p.m.

    mfw dp