Red Panda

시간 제한2초메모리 제한2048 MB

요약
직선 위에서 S에서 출발해 각 상자를 그 상자의 열쇠로 열고 사과를 모두 주워 S로 돌아오는 최소 이동 거리를 구한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 구현
정답자
아직 제출이 없습니다

문제

You and your pet red panda live in a one-dimensional world. Your red panda really loves eating apples. There are NN boxes (numbered from 11 to NN), each containing an apple. Box ii is located at point A_iA\_i. Unfortunately, all the boxes are locked. Luckily, you know the location of all keys; key ii that can unlock box ii is located at point B_iB\_i.

Currently, both you and your red panda are at point SS. You want to gather all the apples and bring them back to point SS for your red panda. At any time, you can carry any number of keys and apples.

The distance between two points pp and qq is ∣p−q∣|p - q|. Determine the minimum total distance you need to cover to bring all the NN apples to point SS.

입력

The first line consists of two integers NN SS (1≤N≤100,0001 ≤ N ≤ 100\\, 000; −109≤S≤109-10^9 ≤ S ≤ 10^9).

Each of the next NN lines consists of two integers A_iA\_i B_iB\_i (−109≤A_i,B_i≤109-10^9 ≤ A\_i , B\_i ≤ 10^9).

출력

Output a single integer representing the minimum total distance you need to cover to bring back all the NN apples to point SS.

예제2

  1. 예제 1

    입력
    4 2
    7 9
    -1 4
    7 -7
    1 3
    
    예상 출력
    36
    
  2. 예제 2

    입력
    1 1
    1 1
    
    예상 출력
    0