Under construction

Still being built. Things may change

Special Oreum Climb

Interview

Time limit1sMemory limit1024 MB

Summary
Find the highest y coordinate on any valid path from (0, 0) to (2N, 0) that avoids teacher points, or report -1 if no such path exists.
Level

Medium4 of 10

Topics
Dynamic programming, Implementation
Solved
No attempts yet

Problem

Oreum must be climbed.

The name of the NLCS Jeju dormitory, "Oreum," comes from the Jeju word for a peak or mountain. Once a year, the students of each dormitory climb the oreum that gave their dormitory its name.

The oreum is a triangle with vertices (0,0)(0, 0), (N,N)(N, N), and (2N,0)(2N, 0) in the xyxy plane. You start at (0,0)(0, 0) and must reach (2N,0)(2N, 0).

From (x,y)(x, y), you can move only to (x+1,y+1)(x+1, y+1) or (x+1,y−1)(x+1, y-1). Every position you arrive at must be inside the oreum or on its boundary.

It is easy to get lost on the oreum, so teachers are stationed at MM points where people tend to lose their way. You have not done your homework, so you do not want to meet a teacher. Move while avoiding the teachers.

You also decide to take photos to make the climb memorable. A photo is more beautiful when it is taken at a higher point. Specifically, the yy coordinate of the place where the photo is taken is its beauty score. Find the maximum beauty score of a photo you can take.

Input

The first line contains NN, the height of the oreum, and MM, the number of points where teachers are, separated by a space.

Lines 2 to M+1M+1 each contain the xx coordinate x_ix\_i and the yy coordinate y_iy\_i of a teacher, separated by a space. Every teacher is inside the oreum or on its boundary.

Output

Print the maximum beauty score of a photo you can take on the first line. If you cannot travel from the start to the destination, print -1.

Constraints

  • 1≤N≤1 0001 \le N \le 1\,000
  • 0≤M≤min⁡((N+1)(N+2)2−1,100 000)0 \le M \le \min\left(\frac{(N+1)(N+2)}{2} - 1, 100\,000 \right)
  • 0≤x_i≤2N0 \le x\_i \le 2N
  • 0≤y_i≤N0 \le y\_i \le N
  • x_i+y_ix\_i + y\_i is even.
  • There is no teacher at (0,0)(0,0).

Examples3

  1. Example 1

    Input
    4 3
    2 2
    4 0
    5 3
    
    Expected output
    2
    
  2. Example 2

    Input
    3 1
    3 1
    
    Expected output
    3
    
  3. Example 3

    Input
    3 1
    1 1
    
    Expected output
    -1