#pragma optimization_level 3
#include <bits/stdc++.h>
#define  f               first
#define  s               second
#define  sym(s)          s = "#" + s + "#";
#define  all(x)          (x).begin(), (x).end()
#define  alll(x,n)       x+1, x+n+1
#define  newl            cout<<"\n";
#define  foo(i,a,n)      for(ll i = (a); i <= n; i++)
#define  deb1(a)         cout<<a<<"\n";
#define  deb2(a,b)       cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)     cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)   cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  debp(a)         cout<<a.f<<" "<<a.s<<"\n";
#define  debv(a)         for(auto it: a)cout<<it<<" ";newl;
#define  debm(a)         for(auto it: a)cout<<"{"<<it.f<<","<<it.s<<"}, ";newl;
#define  deb1d(a,n)      foo(i,1,n)cout<<a[i]<<" ";newl;
#define  deb2d(a,n,m)    foo(i,1,n){foo(j,1,m){cout<<a[i][j]<<" ";}newl;}

using namespace std;
using ll              =  long long;
using ld              =  long double;
const ll   MOD        =  1e+9 + 7;
const ll   INF        =  LLONG_MAX;
const int  N          =  (int)2e+5 + 8;

ll n, m, t, x;
class segment_tree{
public:
    ll n;
    vector<ll> seg, lazy;
    
    segment_tree(ll nn){
        n = nn;
        seg = lazy = vector<ll>(4*n + 8, INF);
        build(1, 1, n);
    }

    ll left(ll nd) {return nd << 1;}
    ll right(ll nd) {return (nd << 1) + 1;}
    ll combine(ll a, ll b) {return min(a,b);}
     
    void push_down(ll node, ll st, ll en, ll val){
        seg[node] = min(seg[node], val);
        if(st != en){
            lazy[left(node)] = min(lazy[left(node)], val);
            lazy[right(node)] = min(lazy[right(node)], val);
        }
        lazy[node] = INF;
    }
     
    void build(ll node, ll st, ll en) {
        if (st == en) {
            seg[node] = n + 1;
            return;
        }
     
        ll mid = (st + en) / 2;
        build(left(node), st, mid);
        build(right(node), mid + 1, en);
        seg[node] = combine(seg[left(node)], seg[right(node)]);
    }
     
    void update(ll node, ll st, ll en, ll l, ll r, ll val){
        //not up to date
        if(lazy[node] != INF) 
            push_down(node, st, en, lazy[node]);
        //full-overlap
        if (l <= st && en <= r) {
            push_down(node, st, en, val);
            return;
        }
        //no-overlap
        if (st > r || en < l)
            return;
     
        //partial-overlap
        ll mid = (st + en) / 2;
        update(left(node), st, mid, l, r, val);
        update(right(node), mid+1, en, l, r, val);
        seg[node] = combine(seg[left(node)], seg[right(node)]);
    }
     
     
    ll query(ll node, ll st, ll en, ll l, ll r) {
        //not up to date
        if(lazy[node] != INF) 
            push_down(node, st, en, lazy[node]);
        //full-overlap
        if (l <= st && en <= r) 
            return seg[node];
        //no-overlap
        if (st > r || en < l) 
            return INF;
     
        //partial-overlap
        ll mid = (st + en) / 2;
        ll lq = query(left(node), st, mid, l, r);
        ll rq = query(right(node), mid + 1, en, l, r);
        
        return combine(lq, rq);
    }
};

void MAIN(int tc){
    cin >> n >> m;
    segment_tree row(n - 2), col(n - 2);
    vector<ll> pre(n + 2, 0);
    for(int i = 1; i <= m; i++){
        cin >> t >> x;
        x--;
        //column query
        if(t == 1){
            ll b = row.query(1, 1, n - 2, x, x);
            pre[x] += -(b-1);
            pre[x + 1] += (b-1);
            col.update(1, 1, n - 2, 1, b - 1, x); 
        }// row query
        else{
            ll b = col.query(1, 1, n - 2, x, x);
            pre[1] += -1;
            pre[b] += +1;
            row.update(1, 1, n - 2, 1, b - 1, x);
        }
    }
    ll res = 0;
    for(int i = 1; i <= n - 2;i ++) {
        pre[i] += pre[i-1];
        res += pre[i] + n - 2;
    }
    deb1(res);
}

int main(){
    ios:: sync_with_stdio(false); 
    cin.tie(nullptr); cout.tie(nullptr);
    cout<<fixed; cout<<setprecision(10);

    int test_cases = 1;
    //cin>>test_cases;
    for(int i = 1; i <= test_cases; i++){
        MAIN(i);
    }
}

Measured memory_bytes: 29828