Skip to content

Going to School

A2400
English

Language

Contribute a translation
Time limit
1000 ms
Memory limit
1024 MB
Submissions
0
Correct
0
Solved by
0
AC rate
Translated by _junick.

Statement

Alex has to arrive at school within $X$ minutes to avoid consequences. To get there, he can take one of $N$ buses that stop at a nearby bus stop.

Alex doesn't want to arrive at school too early, so he looked up the schedules of all $N$ buses. For each bus, he knows how many minutes from now the bus will depart from the bus stop and how many minutes it takes to reach the school.

Determine the latest time at which Alex can board a bus and still arrive at school on time. If no bus can get him to school in time, report that it is impossible.

Constraints

  • All numbers in the input are integers.
  • $1 \leq N \leq 100$
  • $2 \leq X \leq 200$
  • For each bus, let $S$ be the number of minutes until the bus departs from the bus stop, and let $T$ be the number of minutes it takes for the bus to travel from the bus stop to the school.
    • $1 \leq S \leq 100$
    • $1 \leq T \leq 100$

Subtasks

  1. (10 points) $N = 1$
  2. (15 points) All buses arrive at the school within $X$ minutes.
  3. (30 points) $T = 20$ for all buses.
  4. (45 points) No additional constraints.

Input

The first line contains $N$ and $X$, separated by a space.

The next $N$ lines each contain two integers $S$ and $T$, separated by a space. $S$ is the number of minutes until the bus departs from the bus stop, and $T$ is the number of minutes it takes for the bus to travel from the bus stop to the school.

Output

If it is impossible to arrive at the school within $X$ minutes, output $-1$.

Otherwise, output the number of minutes until the latest bus that allows Alex to arrive at the school within $X$ minutes departs.

Examples

Sample input 1
1 30
15 20
Sample output 1
-1
Sample input 2
3 8
2 1
6 3
4 4
Sample output 2
4

Tags

No tags yet

Source