atcoder#ABC203E. [ABC203E] White Pawn

[ABC203E] White Pawn

Score : 500500 points

Problem Statement

Let NN be a positive integer. We have a (2N+1)×(2N+1)(2N+1)\times (2N+1) grid where rows are numbered 00 through 2N2N and columns are also numbered 00 through 2N2N. Let (i,j)(i,j) denote the square at Row ii and Column jj.

We have one white pawn, which is initially at (0,N)(0, N). Also, we have MM black pawns, the ii-th of which is at (Xi,Yi)(X_i, Y_i).

When the white pawn is at (i,j)(i, j), you can do one of the following operations to move it:

  • If 0i2N10\leq i\leq 2N-1, 0j2N0 \leq j\leq 2N hold and (i+1,j)(i+1,j) does not contain a black pawn, move the white pawn to (i+1,j)(i+1, j).
  • If 0i2N10\leq i\leq 2N-1, 0j2N10 \leq j\leq 2N-1 hold and (i+1,j+1)(i+1,j+1) does contain a black pawn, move the white pawn to (i+1,j+1)(i+1,j+1).
  • If 0i2N10\leq i\leq 2N-1, 1j2N1 \leq j\leq 2N hold and (i+1,j1)(i+1,j-1) does contain a black pawn, move the white pawn to (i+1,j1)(i+1,j-1).

You cannot move the black pawns.

Find the number of integers YY such that it is possible to have the white pawn at (2N,Y)(2N, Y) by repeating these operations.

Constraints

  • 1N1091 \leq N \leq 10^9
  • 0M2×1050 \leq M \leq 2\times 10^5
  • 1Xi2N1 \leq X_i \leq 2N
  • 0Yi2N0 \leq Y_i \leq 2N
  • (Xi,Yi)(Xj,Yj)(X_i, Y_i) \neq (X_j, Y_j) (ij)(i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

X1X_1 Y1Y_1

::

XMX_M YMY_M

Output

Print the answer.

2 4
1 1
1 2
2 0
4 2
3

We can move the white pawn to (4,0)(4,0), (4,1)(4,1), and (4,2)(4,2), as follows:

  • (0,2)(1,1)(2,1)(3,1)(4,2)(0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,2)
  • (0,2)(1,1)(2,1)(3,1)(4,1)(0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,1)
  • (0,2)(1,1)(2,0)(3,0)(4,0)(0,2)\to (1,1)\to (2,0)\to (3,0)\to (4,0)

On the other hand, we cannot move it to (4,3)(4,3) or (4,4)(4,4). Thus, we should print 33.

1 1
1 1
0

We cannot move the white pawn from (0,1)(0,1).