#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPr(i, n) for(int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for(int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for(int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
    cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
    int x12345;                                                                \
    cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;

///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////
/// ⸜( ⌓̈ )⸝ ///
//get min interval
//add interval
const ll INF = 1e16;
struct LazySegmentTree {

 private:
     ll n;
     vector<ll> node, lazy;
     vector<bool> lazyFlag;
  
 public:
     LazySegmentTree(vector<ll> v) {
         ll sz = (ll) v.size();
         n = 1;
         while (n < sz) n *= 2;
         node.resize(2 * n - 1);
         lazy.resize(2 * n - 1, INF);
         lazyFlag.resize(2 * n - 1, false);
  
         for (ll i = 0; i < sz; i++) node[i + n - 1] = v[i];
         for (ll i = n - 2; i >= 0; i--) node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
     }
  
     void lazyEvaluate(ll k, ll l, ll r) {
         if (lazyFlag[k]) {
             node[k] = min(node[k], lazy[k]);
             if (r - l > 1) {
                 lazy[k * 2 + 1] = min(lazy[k * 2 + 1], lazy[k]);
                 lazy[k * 2 + 2] = min(lazy[k * 2 + 2], lazy[k]);
                 lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
             }
             lazyFlag[k] = false;
         }
     }
  
     void update(ll a, ll b, ll x, ll k = 0, ll l = 0, ll r = -1) {
         if (r < 0) r = n;
         lazyEvaluate(k, l, r);
         if (b <= l || r <= a) return;
         if (a <= l && r <= b) {
             lazy[k] = min(lazy[k], x);
             lazyFlag[k] = true;
             lazyEvaluate(k, l, r);
         } else {
             update(a, b, x, 2 * k + 1, l, (l + r) / 2);
             update(a, b, x, 2 * k + 2, (l + r) / 2, r);
             node[k] = min(node[2 * k + 1], node[2 * k + 2]);
         }
     }
  
     ll find(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
         if (r < 0) r = n;
         lazyEvaluate(k, l, r);
         if (b <= l || r <= a) return INF;
         if (a <= l && r <= b) return node[k];
         ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
         ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
         return min(vl, vr);
     }
};

int main() {
    ll n,Q;
    ll xl,yl;
    cin >> n >> Q;
    xl = n - 1;
    yl = n - 1;
    ll cnt = (n - 2) * (n - 2);
    vector<ll> x(n, n - 1);
    vector<ll> y(n, n - 1);
    LazySegmentTree segX(x),segY(y);

    while(Q--){
        ll t,x; cin >> t >> x;
        x--;
        if (t == 1){
            ll M = segY.find(x,x+1);
            cnt -= M - 1;
            segX.update(0,M + 1,x);
            //DB(M);
        }else{
            ll M = segX.find(x,x+1);
            cnt -= M - 1;
            //DB(M);
            segY.update(0,M + 1,x);
        }
        //DB(cnt);
    }
    cout << cnt << endl;
}

Measured memory_bytes: 24560