Red Panda
시간 제한2초메모리 제한2048 MB
직선 위에서 S에서 출발해 각 상자를 그 상자의 열쇠로 열고 사과를 모두 주워 S로 돌아오는 최소 이동 거리를 구한다.
문제
You and your pet red panda live in a one-dimensional world. Your red panda really loves eating apples. There are boxes (numbered from to ), each containing an apple. Box is located at point . Unfortunately, all the boxes are locked. Luckily, you know the location of all keys; key that can unlock box is located at point .
Currently, both you and your red panda are at point . You want to gather all the apples and bring them back to point for your red panda. At any time, you can carry any number of keys and apples.
The distance between two points and is . Determine the minimum total distance you need to cover to bring all the apples to point .
입력
The first line consists of two integers (; ).
Each of the next lines consists of two integers ().
출력
Output a single integer representing the minimum total distance you need to cover to bring back all the apples to point .