Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = lambda: sys.stdin.readline().rstrip()

class Segtree():
    def __init__(self, nn, A = []):
        self.inf = 10 ** 9 ###########
        self.size = 1 << nn
        n = self.size * 2
        self.S = [0] * n
        self.ma = [0] * n
        self.mi = [0] * n
        self.mac = [1] * n
        self.mic = [1] * n
        self.sma = [-self.inf] * n
        self.smi = [self.inf] * n
        self.lazy = [0] * n
        for i, a in enumerate(A):
            self.node_set(i, a)
        for i in range(len(A), self.size):
            self.node_set(i, 0)
        self.update_all()

    def node_chmax(self, i, x):
        self.S[i] += (x - self.ma[i]) * self.mac[i]
        if self.ma[i] == self.mi[i]:
            self.ma[i] = x
            self.mi[i] = x
        elif self.ma[i] == self.smi[i]:
            self.ma[i] = x
            self.smi[i] = x
        else:
            self.ma[i] = x
    
    def node_chmin(self, i, x):
        self.S[i] += (x - self.mi[i]) * self.mic[i]
        if self.ma[i] == self.mi[i]:
            self.ma[i] = x
            self.mi[i] = x
        elif self.sma[i] == self.mi[i]:
            self.sma[i] = x
            self.mi[i] = x
        else:
            self.mi[i] = x
    
    def node_set(self, i, x):
        i += self.size
        self.ma[i] = x
        self.mi[i] = x
        self.S[i] = x
    
    def debug(self):
        print("sum =", self.S)
        print("ma =", self.ma)
        print("mi =", self.mi)
        print("mac =", self.mac)
        print("mic =", self.mic)
        print("sma =", self.sma)
        print("smi =", self.smi)
        print("lazy =", self.lazy)
    
    def update_all(self):
        for i in range(self.size - 1, 0, -1):
            self.update(i)
    
    def update(self, i):
        c1 = i * 2
        c2 = c1 | 1
        self.S[i] = self.S[c1] + self.S[c2]
        if self.ma[c1] < self.ma[c2]:
            self.ma[i] = self.ma[c2]
            self.mac[i] = self.mac[c2]
            self.sma[i] = max(self.ma[c1], self.sma[c2])
        elif self.ma[c1] > self.ma[c2]:
            self.ma[i] = self.ma[c1]
            self.mac[i] = self.mac[c1]
            self.sma[i] = max(self.sma[c1], self.ma[c2])
        else:
            self.ma[i] = self.ma[c1]
            self.mac[i] = self.mac[c1] + self.mac[c2]
            self.sma[i] = max(self.sma[c1], self.sma[c2])
        
        if self.mi[c1] < self.mi[c2]:
            self.mi[i] = self.mi[c1]
            self.mic[i] = self.mic[c1]
            self.smi[i] = min(self.smi[c1], self.mi[c2])
        elif self.mi[c1] > self.mi[c2]:
            self.mi[i] = self.mi[c2]
            self.mic[i] = self.mic[c2]
            self.smi[i] = min(self.mi[c1], self.smi[c2])
        else:
            self.mi[i] = self.mi[c1]
            self.mic[i] = self.mic[c1] + self.mic[c2]
            self.smi[i] = min(self.smi[c1], self.smi[c2])
    
    def range_sum(self, a, b, i=1, l=0, r=-1):
        if r == -1: r = self.size
        if r <= a or b <= l: return 0
        if a <=l and r <= b: return self.S[i]
        self.push(i, r - l)
        return self.range_sum(a, b, i * 2, l, (l + r) // 2) + self.range_sum(a, b, i * 2 | 1, (l + r) // 2, r)
    
    def push(self, i, le):
        if i >= self.size: return
        c1 = i * 2
        c2 = c1 | 1
        if self.lazy[i] != 0:
            self.node_add(c1, le // 2, self.lazy[i])
            self.node_add(c2, le // 2, self.lazy[i])
            self.lazy[i] = 0
        if self.ma[c1] > self.ma[i]: self.node_chmax(c1, self.ma[i])
        if self.ma[c2] > self.ma[i]: self.node_chmax(c2, self.ma[i])
        if self.mi[c1] < self.mi[i]: self.node_chmin(c1, self.mi[i])
        if self.mi[c2] < self.mi[i]: self.node_chmin(c2, self.mi[i])
    
    def node_add(self, i, le, x):
        self.ma[i] += x
        if self.sma[i] != - self.inf:
            self.sma[i] += x
        self.mi[i] += x
        if self.smi[i] != self.inf:
            self.smi[i] += x
        self.S[i] += x * le
        self.lazy[i] += x
    
    def range_chmax(self, a, b, x, i=1, l=0, r=-1):
        if r == -1: r = self.size
        if r <= a or b <= l or self.mi[i] >= x: return
        if a <= l and r <= b and self.smi[i] > x:
            self.node_chmin(i, x)
            return
        self.push(i, r - l)
        self.range_chmax(a, b, x, i * 2, l, (l + r) // 2)
        self.range_chmax(a, b, x, i * 2 | 1, (l + r) // 2, r)
        self.update(i)
    
    def range_chmin(self, a, b, x, i=1, l=0, r=-1):
        if r == -1: r = self.size
        if r <= a or b <= l or self.ma[i] <= x: return
        if a <= l and r <= b and self.sma[i] < x:
            self.node_chmax(i, x)
            return
        self.push(i, r - l)
        self.range_chmin(a, b, x, i * 2, l, (l + r) // 2)
        self.range_chmin(a, b, x, i * 2 | 1, (l + r) // 2, r)
        self.update(i)
    
    def range_add(self, a, b, x, i=1, l=0, r=-1):
        if r == -1: r = self.size
        if r <= a or b <= l: return
        if a <= l and r <= b:
            self.node_add(i, r - l, x)
            return
        self.push(i, r - l)
        self.range_add(a, b, x, 2 * i, l, (l + r) // 2)
        self.range_add(a, b, x, 2 * i | 1, (l + r) // 2, r)
        self.update(i)
    
    def debug_all(self):
        print("X =", [self.range_sum(i, i+1) for i in range(self.size)])

N, Q = map(int, input().split())
ans = (N - 2) ** 2
st1 = Segtree(18, [N] * N)
st2 = Segtree(18, [N] * N)

for _ in range(Q):
    t, x = map(int, input().split())
    if t == 1:
        a = st2.range_sum(x, x + 1)
        ans -= a - 2
        st1.range_chmin(1, a, x)
    else:
        a = st1.range_sum(x, x + 1)
        ans -= a - 2
        st2.range_chmin(1, a, x)
print(ans)


Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()
class SegmentTreeDual():
    def __init__(self, init, initX, unitA, g, h):
        self.g = g # (X, A, size) -> X
        self.h = h # (A, A) -> A
        self.unitA = unitA
        if type(init) == int:
            self.n = init
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [initX] * (self.n * 2)
            self.size = [1] * (self.n * 2)
        else:
            self.n = len(init)
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [initX] * self.n + init + [initX] * (self.n - len(init))
            self.size = [0] * self.n + [1] * len(init) + [0] * (self.n - len(init))
    
        for i in range(self.n - 1, 0, -1):
            self.size[i] = self.size[i*2] + self.size[i*2|1]
        
        self.A = [unitA] * (self.n * 2)
        
    def calc(self, i):
        return self.g(self.X[i], self.A[i], self.size[i])
    
    def propagate(self, i):
        self.X[i] = self.g(self.X[i], self.A[i], self.size[i])
        self.A[i*2] = self.h(self.A[i*2], self.A[i])
        self.A[i*2|1] = self.h(self.A[i*2|1], self.A[i])
        self.A[i] = self.unitA
        
    def propagate_above(self, i):
        H = i.bit_length()
        for h in range(H, 0, -1):
            self.propagate(i >> h)
    
    def propagate_all(self):
        for i in range(1, self.n):
            self.propagate(i)
    
    def getvalue(self, i):
        i += self.n
        self.propagate_above(i)
        return self.calc(i)
    
    def operate_range(self, l, r, a):
        l += self.n
        r += self.n
        l0, r0 = l // (l & -l), r // (r & -r) - 1
        self.propagate_above(l0)
        self.propagate_above(r0)
        while l < r:
            if l & 1:
                self.A[l] = self.h(self.A[l], a)
                l += 1
            if r & 1:
                r -= 1
                self.A[r] = self.h(self.A[r], a)
            l >>= 1
            r >>= 1
    
    def check(self, randX, randA, maxs, rep):
        from random import randrange
        for _ in range(rep):
            x = randX()
            a = randA()
            b = randA()
            c = randA()
            s = randrange(1, maxs + 1)
            err = 0
            if not h(a, unitA) == h(unitA, a) == a:
                err = 1
                print("!!!!! unitA Error !!!!!")
                print("unitA =", unitA)
                print("a =", a)
                print("f(a, unitA) =", f(a, unitA))
                print("f(unitA, a) =", f(unitA, a))
                
            if not h(h(a, b), c) == h(a, h(b, c)):
                err = 1
                print("!!!!! Associativity Error A !!!!!")
                print("a, b, c, h(a, b), h(b, c) =", a, b, c, h(a, b), h(b, c))
                print("h(h(a, b), c) =", h(h(a, b), c))
                print("h(a, h(b, c)) =", h(a, h(b, c)))
            
            if not g(x, unitA, s) == x:
                err = 1
                print("!!!!! Identity Error !!!!!")
                print("unitA, x, s =", unitA, x, s)
                print("g(x, unitA, s) =", g(x, unitA, s))
            
            if not g(g(x, a, s), b, s) == g(x, h(a, b), s):
                err = 1
                print("!!!!! Act Error !!!!!")
                print("x, a, b, s, g(x, a, s), h(a, b) =", x, a, b, s, g(x, a, s), h(a, b))
                print("g(g(x, a, s), b, s) =", g(g(x, a, s), b, s))
                print("g(x, h(a, b), s)    =", g(x, h(a, b), s))
            
            if err:
                break
                assert h(a, unitA) == h(unitA, a) == a
                assert h(h(a, b), c) == h(a, h(b, c))
                assert g(x, unitA, s) == x
                assert g(g(x, a, s), b, s) == g(x, h(a, b), s)
        else:
            pass
            print("Monoid Check OK!")
    
    def debug(self):
        print("self.n =", self.n)
        deX = []
        deA = []
        deS = []
        a, b = self.n, self.n * 2
        while b:
            deX.append(self.X[a:b])
            deA.append(self.A[a:b])
            deS.append(self.size[a:b])
            a, b = a//2, a
        print("--- debug ---")
        for d in deX[::-1]:
            print(d)
        print("--- ---")
        for d in deA[::-1]:
            print(d)
        print("--- ---")
        for d in deS[::-1]:
            print(d)
        print("--- ---")

N, Q = map(int, input().split())

g = lambda x, a, s: min(x, a)
h = lambda a, b: min(a, b)
unitA = 1 << 30
initX = N
st1 = SegmentTreeDual(N, initX, unitA, g, h)
st2 = SegmentTreeDual(N, initX, unitA, g, h)

if False:
    from random import randrange
    randX = lambda: randrange(-10, 11)
    randA = lambda: [-1, randrange(21)][randrange(2)]
    maxs = 10
    rep = 1000
    st1.check(randX, randA, maxs, rep)
    st2.check(randX, randA, maxs, rep)

    print("END")

ans = (N - 2) ** 2
for _ in range(Q):
    t, x = map(int, input().split())
    if t == 1:
        a = st2.getvalue(x)
        ans -= a - 2
        st1.operate_range(1, a, x)
    else:
        a = st1.getvalue(x)
        ans -= a - 2
        st2.operate_range(1, a, x)
    # print("_, a, ans =", _, a, ans)

print(ans)