Airplane Passengers

No attempts yetTime limit1sMemory limit1024 MB

Problem

Every weekend a plane flies from Bitland to Vilnius, and its passengers are extremely demanding: they keep asking the crew for tea, a pillow, and so on. A single flight attendant must fulfill every request.

The rows of the cabin are numbered 1,2,3,1, 2, 3, \dots. The attendant starts the flight (at minute 00) standing at row 11. Walking from one row to an adjacent row takes exactly 11 minute, and fulfilling a request itself takes no time (it is instantaneous).

Each request is given by two integers (a,b)(a, b): the passenger sits in row aa, and the request is made no earlier than minute bb. Time is measured in minutes, starting from minute 00 at the beginning of the flight. A request may be fulfilled at minute bb or at any later minute, but never before minute bb. To fulfill a request, the attendant must be standing in that passenger's row at a time of at least bb.

The attendant may finish the flight standing at any row. Planning her movements optimally, find the minimum number of minutes needed to fulfill every request.

Input

The first line contains the number of requests NN.

Each of the next NN lines contains two integers aia_i and bib_i separated by a space, describing one request. Here aia_i is the number of the row where the passenger sits, and bib_i is the earliest time at which the ii-th request may appear (it may be fulfilled later than this).

Output

Print a single integer: the minimum number of minutes the attendant needs in order to fulfill every request.

Constraints

  • 1N10001 \le N \le 1000
  • 1ai,bi1061 \le a_i, b_i \le 10^6