output_description: Print how many black stones there are on the grid after processing all Q
  queries.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N Q
      Query_1
      \vdots
      Query_Q
attempt: import sys
  
  sys.setrecursionlimit(500000)
  
  
  def input():
      return sys.stdin.readline()[:-1]
  
  
  class SegmentTree:
      def __init__(self, L, op, ide):
          self.op = op
          self.ide = ide
          self.sz = len(L)
          self.n = 1
          self.s = 1
          for i in range(1000):
              self.n *= 2
              self.s += 1
              if self.n >= self.sz:
                  break
          self.node = [self.ide] * (2 * self.n - 1)
  
          for i in range(self.sz):
              self.node[i + self.n - 1] = L[i]
  
          for i in range(self.n - 2, -1, -1):
              self.node[i] = self.op(self.node[i * 2 + 1], self.node[i * 2 + 2])
  
      def add(self, a, x):
          k = a + self.n - 1
          self.node[k] += x
          for i in range(1000):
              k = (k - 1) // 2
              self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
              if k <= 0:
                  break
  
      def substitute(self, a, x):
          k = a + self.n - 1
          self.node[k] = self.op(x, self.node[k])
          for i in range(1000):
              k = (k - 1) // 2
              self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
              if k <= 0:
                  break
  
      def get_one(self, a):
          k = a + self.n - 1
          return self.node[k]
  
      def get(self, l, r):
          res = self.ide
          n = self.n
          if self.sz <= r or 0 > l:
              return False
  
          for i in range(self.s):
              count = 2**i - 1
              a = (r + 1) // n
              b = (l - 1) // n
              if a - b == 3:
                  res = self.op(self.node[count + b + 1], res)
                  res = self.op(self.node[count + b + 2], res)
                  right = a * n
                  left = (b + 1) * n - 1
                  break
              if a - b == 2:
                  res = self.op(self.node[count + b + 1], res)
                  right = a * n
                  left = (b + 1) * n - 1
                  break
              n = n // 2
  
          # left
          n1 = n // 2
          for j in range(i + 1, self.s):
              count = 2**j - 1
              a = (left + 1) // n1
              b = (l - 1) // n1
              if a - b == 2:
                  res = self.op(self.node[count + b + 1], res)
                  left = (b + 1) * n1 - 1
              n1 = n1 // 2
  
          # right
          n1 = n // 2
          for j in range(i + 1, self.s):
              count = 2**j - 1
              a = (r + 1) // n1
              b = (right - 1) // n1
              if a - b == 2:
                  res = self.op(self.node[count + b + 1], res)
                  right = a * n1
              n1 = n1 // 2
          return res
  
  
  def smin(a, b):
      if a > b:
          return b
      else:
          return a
  
  
  def main():
      N, Q = list(map(int, input().split()))
  
      ST1 = SegmentTree([N - 2] * (N - 2), smin, N - 2)
      ST2 = SegmentTree([N - 2] * (N - 2), smin, N - 2)
  
      ans = 0
      for i in range(Q):
          a, b = list(map(int, input().split()))
          b -= 2
          if a == 1:
              c = ST1.get(b, N - 3)
              ans += c
              ST2.substitute(c - 1, b)
          else:
              c = ST2.get(b, N - 3)
              ans += c
              ST1.substitute(c - 1, b)
          # print(ans)
          # print(*[ST1.get_one((i)) for i in range(N-2)])
          # print(*[ST2.get_one((i)) for i in range(N-2)])
  
      print((N - 2) ** 2 - ans)
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  There is a grid with N rows and N columns of squares. Let (i, j) be the square
  at the i-th row from the top and the j-th column from the left.
  
  Each of the central (N-2) \times (N-2) squares in the grid has a black stone
  on it. Each of the 2N - 1 squares on the bottom side and the right side has a
  white stone on it.
  
  Q queries are given. We ask you to process them in order. There are two kinds
  of queries. Their input format and description are as follows:
  
    * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone.
    * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone.
  
  How many black stones are there on the grid after processing all Q queries?
samples: [{"input": "5 5\n    1 3\n    2 3\n    1 4\n    2 2\n    1 2", "output": "1\n    \n\nAfter each query, the grid changes in the following way:\n\n![Figure](https://img.atcoder.jp/ghi/31ba2cd6b3155b137f0e007299225028.png)\n\n* * *"}, {"input": "200000 0", "output": "39999200004\n    \n\n* * *"}, {"input": "176527 15\n    1 81279\n    2 22308\n    2 133061\n    1 80744\n    2 44603\n    1 170938\n    2 139754\n    2 15220\n    1 172794\n    1 159290\n    2 156968\n    1 56426\n    2 77429\n    1 97459\n    2 71282", "output": "31159505795"}]