#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(unused_mut)]
#![allow(unused_variables)]
#![allow(dead_code)]

use itertools::Itertools;
use proconio::input;
use proconio::marker::{Chars, Usize1};

#[allow(unused_macros)]
#[cfg(debug_assertions)]
macro_rules! mydbg {
    //($arg:expr) => (dbg!($arg))
    //($arg:expr) => (println!("{:?}",$arg));
      ($($a:expr),*) => {
          eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
      }
}
#[cfg(not(debug_assertions))]
macro_rules! mydbg {
    ($($arg:expr),*) => {};
}

macro_rules! echo {
    ($($a:expr),*) => {
          $(println!("{}",$a))*
      }
}

use std::cmp::*;
use std::collections::*;
use std::ops::{Add, Div, Mul, Sub};

#[allow(dead_code)]
static INF_I64: i64 = 92233720368547758;
#[allow(dead_code)]
static INF_I32: i32 = 21474836;
#[allow(dead_code)]
static INF_USIZE: usize = 18446744073709551;
#[allow(dead_code)]
static M_O_D: usize = 1000000007;
#[allow(dead_code)]
static PAI: f64 = 3.1415926535897932;

trait IteratorExt: Iterator {
    fn toVec(self) -> Vec<Self::Item>;
}

impl<T: Iterator> IteratorExt for T {
    fn toVec(self) -> Vec<Self::Item> {
        self.collect()
    }
}

trait CharExt {
    fn toNum(&self) -> usize;
    fn toAlphabetIndex(&self) -> usize;
    fn toNumIndex(&self) -> usize;
}
impl CharExt for char {
    fn toNum(&self) -> usize {
        return *self as usize;
    }
    fn toAlphabetIndex(&self) -> usize {
        return self.toNum() - 'a' as usize;
    }
    fn toNumIndex(&self) -> usize {
        return self.toNum() - '0' as usize;
    }
}

trait VectorExt {
    fn joinToString(&self, s: &str) -> String;
}
impl<T: ToString> VectorExt for Vec<T> {
    fn joinToString(&self, s: &str) -> String {
        return self
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(s);
    }
}

trait StringExt {
    fn get_reverse(&self) -> String;
}
impl StringExt for String {
    fn get_reverse(&self) -> String {
        self.chars().rev().collect::<String>()
    }
}

trait UsizeExt {
    fn pow(&self, n: usize) -> usize;
}
impl UsizeExt for usize {
    fn pow(&self, n: usize) -> usize {
        return ((*self as u64).pow(n as u32)) as usize;
    }
}

pub trait OpMonoid {
    type T: Clone + PartialEq;
    fn id() -> Self::T;
    fn op(a: &Self::T, b: &Self::T) -> Self::T;
    fn op2(a: &Self::T, b: &Self::T) -> Self::T;
    fn op3(a: &Self::T, b: &i64) -> Self::T;
}
pub struct LazySegTree<M: OpMonoid> {
    n: usize,
    data: Vec<M::T>,
    lazy: Vec<M::T>,
}
impl<M: OpMonoid> LazySegTree<M> {
    pub fn new(n_: usize) -> LazySegTree<M> {
        let mut n = 1 as usize;
        while (n < n_) {
            n *= 2;
        }
        LazySegTree {
            n: n,
            data: vec![M::id().clone(); (2 * n)],
            lazy: vec![M::id().clone(); (2 * n)],
        }
    }
    #[macro_use]
    pub fn update(&mut self, index: usize, v: M::T) {
        let mut k = index + self.n - 1;
        self.data[k] = v;
        while (k > 0) {
            k = (k - 1) / 2;
            self.data[k] = M::op(&self.data[2 * k + 1], &self.data[2 * k + 2]);
        }
    }
    pub fn eval(&mut self, k: usize, l: i32, r: i32) {
        if self.lazy[k] != M::id().clone() {
            if (r - l) > 1 {
                self.lazy[2 * k + 1] = M::op2(&self.lazy[2 * k + 1], &self.lazy[k]);
                self.lazy[2 * k + 2] = M::op2(&self.lazy[2 * k + 2], &self.lazy[k]);
            }
            self.data[k] = M::op2(&self.data[k], &M::op3(&self.lazy[k], &((r - l) as i64)));
            self.lazy[k] = M::id().clone();
        }
    }
    pub fn update_range_sub(&mut self, a: usize, b: usize, v: M::T, k: usize, l: i32, r: i32) {
        let mut r = r;
        if r < 0 {
            r = self.n as i32;
        }
        self.eval(k, l, r);
        if b as i32 <= l || r <= a as i32 {
            return;
        }
        if a as i32 <= l && r <= b as i32 {
            self.lazy[k] = v.clone();
            self.eval(k, l, r);
        } else {
            self.update_range_sub(a, b, v.clone(), 2 * k + 1, l, (l + r) / 2);
            self.update_range_sub(a, b, v.clone(), 2 * k + 2, (l + r) / 2, r);
            self.data[k] = M::op(&self.data[2 * k + 1], &self.data[2 * k + 2]);
        }
    }
    pub fn update_range(&mut self, l: usize, r: usize, v: M::T) {
        self.update_range_sub(l, r, v, 0, 0, -1);
    }
    pub fn query_sub(&mut self, a: usize, b: usize, k: usize, l: i32, r: i32) -> M::T {
        let mut r = r;
        if r < 0 {
            r = self.n as i32
        };
        if b as i32 <= l || r <= a as i32 {
            return M::id();
        }
        self.eval(k, l, r);
        if a as i32 <= l && r <= b as i32 {
            return self.data[k].clone();
        }
        let vl = self.query_sub(a, b, 2 * k + 1, l, (l + r) / 2);
        let vr = self.query_sub(a, b, 2 * k + 2, (l + r) / 2, r);
        return M::op(&vl, &vr);
    }
    pub fn query(&mut self, l: usize, r: usize) -> M::T {
        self.query_sub(l, r, 0, 0, -1)
    }
}
struct MIN;
impl OpMonoid for MIN {
    type T = u64;
    fn id() -> Self::T {
        u64::max_value()
    }
    fn op(a: &Self::T, b: &Self::T) -> Self::T {
        min(*a, *b)
    }
    fn op2(a: &Self::T, b: &Self::T) -> Self::T {
        min(*a, *b)
    }
    fn op3(a: &Self::T, b: &i64) -> Self::T {
        *a
    }
}

fn main() {
    input! {
       N: usize,
       Q:usize,
    }

    let mut ans = 0;
    let mut yoko: LazySegTree<MIN> = LazySegTree::new(N);
    let mut tate: LazySegTree<MIN> = LazySegTree::new(N);
    yoko.update_range(0, N, N as u64 - 1);
    tate.update_range(0, N, N as u64 - 1);
    for _ in 0..Q {
        input! {
            t:usize,
            p:usize,
        }
        if t == 1 {
            let a = yoko.query(p - 1, p);
            mydbg!(a);
            ans += a;
            tate.update_range(0, a as usize, p as u64 - 1);
        } else {
            let a = tate.query(p - 1, p);
            mydbg!(a);
            ans += a;
            yoko.update_range(0, a as usize, p as u64 - 1);
        }
    }

    let a = N * N;
    let b = N + N - 1;
    let c = N * 2 - 2;
    let ans = 1 + Q + a - b - ans as usize - c;
    echo!(ans);
}


Measured memory_bytes: 20172