#![allow(unused_imports)]
#![allow(unused_macros)]
use itertools::Itertools;
use std::cmp::{max, min};
use std::collections::*;
use std::i64;
use std::io::{stdin, Read};
use std::usize;

trait ChMinMax {
    fn chmin(&mut self, other: Self);
    fn chmax(&mut self, other: Self);
}
impl<T> ChMinMax for T
where
    T: PartialOrd,
{
    fn chmin(&mut self, other: Self) {
        if *self > other {
            *self = other
        }
    }
    fn chmax(&mut self, other: Self) {
        if *self < other {
            *self = other
        }
    }
}

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, $var:ident $($r:tt)*) => {
        let $var = parse_val!($it, usize);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}
use num::{Bounded, One, Zero};
use std::ops::{Add, Mul};
pub trait Monoid {
    fn unit() -> Self;
    fn add(lhs: &Self, rhs: &Self) -> Self;
}

#[derive(Copy, Clone, Debug)]
pub struct Sum<T>(T);

impl<T> Monoid for Sum<T>
where
    T: Zero + Add<Output = T> + Copy,
{
    fn unit() -> Self {
        Self(T::zero())
    }
    fn add(lhs: &Self, rhs: &Self) -> Self {
        Self(lhs.0 + rhs.0)
    }
}

impl<T> From<T> for Sum<T> {
    fn from(x: T) -> Self {
        Self(x)
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Product<T>(pub T);

impl<T: Copy + One + Mul<Output = T>> Monoid for Product<T> {
    fn unit() -> Self {
        Self(T::one())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0 * r.0)
    }
}

impl<T> From<T> for Product<T> {
    fn from(v: T) -> Self {
        Product(v)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Max<T>(pub T);

impl<T: Copy + Ord + Bounded> Monoid for Max<T> {
    fn unit() -> Self {
        Self(<T as Bounded>::min_value())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0.max(r.0))
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Min<T>(pub T);

impl<T: Copy + Ord + Bounded> Monoid for Min<T> {
    fn unit() -> Self {
        Self(<T as Bounded>::max_value())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0.min(r.0))
    }
}

pub struct SegTree<T: Monoid + Clone> {
    dat: Vec<T>,
    n: usize,
}

impl<T: Monoid + Copy> SegTree<T> {
    pub fn new(n: usize) -> Self {
        let dat = vec![T::unit(); n << 1];
        SegTree { dat: dat, n: n }
    }
    pub fn update(&mut self, k: usize, v: T) {
        let mut k = k + self.n;
        self.dat[k] = v;
        while {
            k >>= 1;
            k > 0
        } {
            self.dat[k] = T::add(&self.dat[k << 1], &self.dat[k << 1 | 1]);
        }
    }
    pub fn get(&self, a: usize, b: usize) -> T {
        let mut a = a + self.n;
        let mut b = b + self.n;
        let mut va = T::unit();
        let mut vb = T::unit();
        while a < b {
            if a & 1 != 0 {
                va = T::add(&va, &self.dat[a]);
                a += 1;
            }
            if b & 1 != 0 {
                vb = T::add(&self.dat[b - 1], &vb);
            }
            a >>= 1;
            b >>= 1;
        }
        T::add(&va, &vb)
    }
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, q: usize);
    let mut ret = (n - 2) * (n - 2);
    let mut d = n - 1;
    let mut r = n - 1;
    let mut xs = vec![n - 1; n];
    let mut ys = vec![n - 1; n];
    for _ in 0..q {
        parse!(it, t: usize, x: u1);
        let (xs, ys, r, d) = if t == 1 {
            (&mut xs, &mut ys, &mut r, &mut d)
        } else {
            (&mut ys, &mut xs, &mut d, &mut r)
        };
        if x < *r {
            for i in x..*r {
                xs[i] = *d;
            }
            *r = x;
            ret -= *d - 1;
        } else {
            ret -= xs[x] - 1;
        }
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}


Measured memory_bytes: 6944