14B.md (1452B)
1 Source: https://codeforces.com/problemset/problem/14/B 2 3 B. Young Photographer 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 64 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 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 *x*0 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 *a*1 to position *b*1, the second — from *a*2 to *b*2 22 23 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. 24 25 Input 26 27 The first line of the input file contains integers *n* and *x*0 (1 ≤ *n* ≤ 100; 0 ≤ *x*0 ≤ 1000). The following *n* lines contain pairs of integers *a**i*, *b**i* (0 ≤ *a**i*, *b**i* ≤ 1000; *a**i* ≠ *b**i*). 28 29 Output 30 31 Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1. 32 33 Examples 34 35 Input 36 37 ``` 38 3 3 39 0 7 40 14 2 41 4 6 42 ``` 43 44 Output 45 46 ``` 47 1 48 ```