Time Management

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John has decided to manage his time efficiently. He has numbered his $N$ chores ($1 \le N \le 1000$) — such as milking cows, cleaning the barn, and mending fences.

Completing chore $i$ takes $T_i$ units of time ($1 \le T_i \le 1000$), and it must be finished by its deadline $S_i$ ($1 \le S_i \le 1{,}000{,}000$). John starts his day at $t = 0$, and once he begins a chore he works on nothing else until it is done.

John loves to sleep in. Print the latest time at which he can start working while still finishing every chore by its deadline.

Input

The first line contains the number of chores $N$.

Each of the next $N$ lines contains $T_i$ and $S_i$, separated by a space.

Output

Print the latest time at which John can start working. If he cannot finish all chores on time, print $-1$.