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 os
  import sys
  
  import numpy as np
  
  
  def solve(inp):
      SEGTREE_TABLES = []
      COMMON_STACK = np.zeros(10**7, dtype=np.int64)
      INF = 10**18
  
      def bit_length(n):
          ret = 0
          while n:
              n >>= 1
              ret += 1
          return ret
  
      def segtree_init(n):
          n2 = 1 << bit_length(n)
          table = np.full((n2 << 1, 2), INF, dtype=np.int64)
          SEGTREE_TABLES.append(table)
          return len(SEGTREE_TABLES) - 1
  
      def segtree_build(ins, arr):
          table = SEGTREE_TABLES[ins]
          offset = table.shape[0] >> 1
          table[offset : offset + len(arr), 0] = arr
          for i in range(offset - 1, 0, -1):
              ch = i << 1
              table[i, 0] = min(table[ch, 0], table[ch + 1, 0])
  
      def segtree_eval(table, offset, i):
          lazy_min = table[i, 1]
          if i < offset:
              ch = i << 1
              table[ch, 1] = min(table[ch, 1], lazy_min)
              table[ch + 1, 1] = min(table[ch + 1, 1], lazy_min)
          table[i, 0] = min(table[i, 0], lazy_min)
          table[i, 1] = INF
  
      def segtree_bottomup(table, i):
          lch = i << 1
          rch = lch + 1
          l_dat = min(table[lch, 0], table[lch, 1])
          r_dat = min(table[rch, 0], table[rch, 1])
          table[i, 0] = min(l_dat, r_dat)
  
      def segtree_range_update(ins, l, r, mn):
          table = SEGTREE_TABLES[ins]
          offset = table.shape[0] >> 1
          stack = COMMON_STACK
          stack[:3] = (1, 0, offset)
          si = 3
          updated = []
          while si:
              i, a, b = stack[si - 3 : si]
  
              segtree_eval(table, offset, i)
  
              if b <= l or r <= a:
                  si -= 3
                  continue
  
              if l <= a and b <= r:
                  table[i, 1] = min(table[i, 1], mn)
                  si -= 3
                  continue
  
              updated.append(i)
  
              m = (a + b) // 2
              stack[si - 3 : si] = (i << 1, a, m)
              stack[si : si + 3] = ((i << 1) + 1, m, b)
              si += 3
  
          while updated:
              i = updated.pop()
              segtree_bottomup(table, i)
  
      def segtree_query(ins, l, r):
          """sum [l, r)"""
          table = SEGTREE_TABLES[ins]
          offset = table.shape[0] >> 1
          stack = COMMON_STACK
          stack[:3] = (1, 0, offset)
          si = 3
          res = INF
          updated = []
  
          while si:
              i, a, b = stack[si - 3 : si]
  
              segtree_eval(table, offset, i)
  
              if b <= l or r <= a:
                  si -= 3
                  continue
  
              if l <= a and b <= r:
                  res = min(res, table[i, 0])
                  si -= 3
                  continue
  
              updated.append(i)
  
              m = (a + b) // 2
              stack[si - 3 : si] = (i << 1, a, m)
              stack[si : si + 3] = ((i << 1) + 1, m, b)
              si += 3
  
          while updated:
              i = updated.pop()
              segtree_bottomup(table, i)
  
          return res
  
      def segtree_debug_print(ins):
          table = SEGTREE_TABLES[ins]
          offset = table.shape[0] >> 1
          for t in range(2):
              i = 1
              while i <= offset:
                  print(table[i : 2 * i, t])
                  i <<= 1
  
      n = inp[0]
      q = inp[1]
      ops = inp[2::2]
      xxx = inp[3::2]
  
      ins1 = segtree_init(n - 2)
      ins2 = segtree_init(n - 2)
      init = np.full(n - 2, n - 2, dtype=np.int64)
      segtree_build(ins1, init)
      segtree_build(ins2, init)
  
      ans = (n - 2) * (n - 2)
      for qi in range(q):
          o = ops[qi]
          x = xxx[qi]
          if o == 1:
              k = segtree_query(ins1, x - 2, x - 1)
              ans -= k
              segtree_range_update(ins2, 0, k + 1, x - 2)
          else:
              k = segtree_query(ins2, x - 2, x - 1)
              ans -= k
              segtree_range_update(ins1, 0, k + 1, x - 2)
          # print(qi, o, x, k, ans)
          # segtree_debug_print(ins1)
          # segtree_debug_print(ins2)
  
      return ans
  
  
  if sys.argv[-1] == "ONLINE_JUDGE":
      from numba.pycc import CC
  
      cc = CC("my_module")
      cc.export("solve", "(i8[:],)")(solve)
      cc.compile()
      exit()
  
  if os.name == "posix":
      # noinspection PyUnresolvedReferences
      from my_module import solve
  else:
      from numba import njit
  
      solve = njit("(i8[:],)", cache=True)(solve)
      print("compiled", file=sys.stderr)
  
  inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=" ")
  ans = solve(inp)
  print(ans)
  
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"}]