问题 4831 --拍照

4831: 拍照★★★

时间限制: 1 Sec  内存限制: 128 MB
提交: 24  解决: 16
[提交][状态][命题人:]

题目描述

Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2

What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.

Bob十分热爱摄影,他尤其爱为运动员拍照片。他会站在直跑道的x0坐标处,准备好拍照,但问题是不是所有的运动员会经过他旁边。运动员总数为n,每位运动员都会在跑道上
特定一段区间内训练,例如,第一位运动员在坐标a1到b1之间的区域训练,第二位运动员在坐标a2到b2之间的区域训练。请问为了拍到所有运动员的照片,Bob需要移动的
最小距离是多少?注意,只要Bob站在运动员的训练范围内,他就能拍到这位运动员的照片。

输入

The first line of the input file contains integers n and x0 (1 ≤ n ≤ 1000 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi(0 ≤ ai, bi ≤ 1000aibi).

第一行为n和x0(1=<n<=100;0=<x0<=1000).接下来n行每行为一对数,ai,bi(0=<ai,bi<=1000;ai ≠ bi).

输出

Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.

输出为了拍到所有运动员的照片,Bob需要移动的最小距离。如果不存在能拍到所有运动员照片的位置,输出-1.

样例输入
Copy
3 3
0 7
14 2
4 6
样例输出
Copy
1

提示

来源

[提交][状态]