#include <bits/stdc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
*/
/*
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>;
using namespace boost::multiprecision;
*/
#ifdef LOCAL_TEST
	#define BOOST_STACKTRACE_USE_ADDR2LINE
	#define BOOST_STACKTRACE_ADDR2LINE_LOCATION /usr/local/opt/binutils/bin/addr2line
	#define _GNU_SOURCE 1
	#include <boost/stacktrace.hpp>
	namespace std {
		template<typename T> class dvector : public std::vector<T> {
		public:
			dvector() : std::vector<T>() {}
			explicit dvector(size_t n, const T& value = T()) : std::vector<T>(n, value) {}
			dvector(const std::vector<T>& v) : std::vector<T>(v) {}
			dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
			dvector(const std::string::iterator first, const std::string::iterator last) : std::vector<T>(first, last) {}
			dvector(const typename std::vector<T>::iterator first, const typename std::vector<T>::iterator last) : std::vector<T>(first, last) {}
			dvector(const typename std::vector<T>::reverse_iterator first, const typename std::vector<T>::reverse_iterator last) : std::vector<T>(first, last) {}
			dvector(const typename std::vector<T>::const_iterator first, const typename std::vector<T>::const_iterator last) : std::vector<T>(first, last) {}
			dvector(const typename std::vector<T>::const_reverse_iterator first, const typename std::vector<T>::const_reverse_iterator last) : std::vector<T>(first, last) {}
			T& operator[](size_t n) {
				try { return this->at(n); } catch (const std::exception& e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); }
			}
			const T& operator[](size_t n) const {
				try { return this->at(n); } catch (const std::exception& e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); }
			}
		};
	}
	class dbool {
	private:
		bool boolvalue;
	public:
		dbool() : boolvalue(false) {}
		dbool(bool b) : boolvalue(b) {}
		operator bool&() { return boolvalue; }
		operator const bool&() const { return boolvalue; }
	};
	#define vector dvector
	#define bool dbool
	class SIGFPE_exception : std::exception {};
	class SIGSEGV_exception : std::exception {};
	void catch_SIGFPE([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGFPE_exception(); }
	void catch_SIGSEGV([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGSEGV_exception(); }
	signed convertedmain();
	signed main() { signal(SIGFPE, catch_SIGFPE); signal(SIGSEGV, catch_SIGSEGV); return convertedmain(); }
	#define main() convertedmain()
#endif
#ifdef LOCAL_DEV
	template<typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::pair<T1, T2>& p) {
		return s << "(" << p.first << ", " << p.second << ")"; }
	template <typename T, size_t N> std::ostream& operator<<(std::ostream& s, const std::array<T, N>& a) {
		s << "{ "; for (size_t i = 0; i < N; ++i){ s << a[i] << "\t"; } s << "}"; return s; }
	template<typename T> std::ostream& operator<<(std::ostream& s, const std::set<T>& se) {
		s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; }
	template<typename T> std::ostream& operator<<(std::ostream& s, const std::multiset<T>& se) {
		s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; }
	template<typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::map<T1, T2>& m) {
		s << "{\n"; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << "\n"; } s << "}"; return s; }
	template<typename T> std::ostream& operator<<(std::ostream& s, const std::deque<T>& v) {
		for (size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; }
	template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) {
		for (size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; }
	template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<std::vector<T>>& vv) {
		s << "\\\n"; for (size_t i = 0; i < vv.size(); ++i){ s << vv[i] << "\n"; } return s; }
	void debug_impl() { std::cerr << '\n'; }
	template<typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << " " << head << (sizeof...(tail) ? "," : ""); debug_impl(tail...); }
	#define debug(...) do { std::cerr << ":" << __LINE__ << " (" << #__VA_ARGS__ << ") ="; debug_impl(__VA_ARGS__); } while (false)
#else
	#define debug(...) do {} while (false)
#endif
//#define int long long
using ll = long long;
//constexpr int INF = (ll)1e9 + 7; //INT_MAX = (1<<31)-1 = 2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1 = 9223372036854775807
constexpr ll MOD = (ll)1e9 + 7; //primitive root = 5
//constexpr ll MOD = 998244353; //primitive root = 3
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n)   for(ll i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)
#define rrep(i, n)   for(ll i=(n)-1; i>=0; --i)
#define rrepeq(i, n) for(ll i=(n)  ; i>=1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template<typename Head, typename... Tail> void p(Head head, Tail... tail) { std::cout << head << (sizeof...(tail) ? " " : ""); p(tail...); }
template<typename T> inline void pv(std::vector<T>& v) { for(ll i=0, N=v.size(); i<N; i++) std::cout << v[i] << " \n"[i==N-1]; }
template<typename T> inline bool chmax(T& a, T b) { return a < b && (a = b, true); }
template<typename T> inline bool chmin(T& a, T b) { return a > b && (a = b, true); }
template<typename T> inline void uniq(std::vector<T>& v) { v.erase(std::unique(v.begin(), v.end()), v.end()); }

/*-----8<-----template-----8<-----*/

/**
 * @brief a segment tree beats (range {chmin, chmax, add, update} + range {min, max, sum})
 * @docs data_structure/segment_tree_beats.md
 * @see https://codeforces.com/blog/entry/57319
 */
class SegmentTreeBeats {
	// MEMO: values for queries (max, min, lazy_add, and lazy_update) already apply to the current node; but not for children
	typedef struct {
		ll max;
		ll max_second;
		int max_count;
		ll min;
		ll min_second;
		int min_count;
		ll lazy_add;
		ll lazy_update;
		ll sum;
	} value_type;

public:
	int n;
	vector<value_type> a;

	SegmentTreeBeats() = default;
	SegmentTreeBeats(int n_) {
		n = 1; while (n < n_) n *= 2;
		a.resize(2 * n - 1);
		tag<UPDATE>(0, 0);
	}
	template <class InputIterator>
	SegmentTreeBeats(InputIterator first, InputIterator last) {
		int n_ = std::distance(first, last);
		n = 1; while (n < n_) n *= 2;
		a.resize(2 * n - 1);
		for(int i=0; i<n_; i++){
			tag<UPDATE>(n - 1 + i, *(first + i));
		}
		for(ll i=n_; i<n; i++){
			tag<UPDATE>(n - 1 + i, 0);
		}
		for(ll i=n-2; i>=0; i--){
			update(i);
		}
	}

	void range_chmin(int l, int r, ll value) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		range_apply<CHMIN>(0, 0, n, l, r, value);
	}
	void range_chmax(int l, int r, ll value) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		range_apply<CHMAX>(0, 0, n, l, r, value);
	}
	void range_add(int l, int r, ll value) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		range_apply<ADD>(0, 0, n, l, r, value);
	}
	void range_update(int l, int r, ll value) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		range_apply<UPDATE>(0, 0, n, l, r, value);
	}

	ll range_min(int l, int r) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		return range_get<MIN>(0, 0, n, l, r);
	}
	ll range_max(int l, int r) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		return range_get<MAX>(0, 0, n, l, r);
	}
	ll range_sum(int l, int r) {  // 0-based, [l, r)
		assert (0 <= l and l <= r and r <= n);
		return range_get<SUM>(0, 0, n, l, r);
	}

private:
	static constexpr char CHMIN = 0;
	static constexpr char CHMAX = 1;
	static constexpr char ADD = 2;
	static constexpr char UPDATE = 3;
	static constexpr char MIN = 10;
	static constexpr char MAX = 11;
	static constexpr char SUM = 12;

	template <char TYPE>
	void range_apply(int i, int il, int ir, int l, int r, ll g) {
		if (ir <= l or r <= il or break_condition<TYPE>(i, g)) {
			// break
		} else if (l <= il and ir <= r and tag_condition<TYPE>(i, g)) {
			tag<TYPE>(i, g);
		} else {
			pushdown(i);
			range_apply<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r, g);
			range_apply<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r, g);
			update(i);
		}
	}
	template <char TYPE>
	inline bool break_condition(int i, ll g) {
		switch (TYPE) {
			case CHMIN: return a[i].max <= g;
			case CHMAX: return g <= a[i].min;
			case ADD: return false;
			case UPDATE: return false;
			default: assert (false);
		}
	}
	template <char TYPE>
	inline bool tag_condition(int i, ll g) {
		switch (TYPE) {
			case CHMIN: return a[i].max_second < g and g < a[i].max;
			case CHMAX: return a[i].min < g and g < a[i].min_second;
			case ADD: return true;
			case UPDATE: return true;
			default: assert (false);
		}
	}
	template <char TYPE>
	inline void tag(int i, ll g) {
		int length = n >> (32 - __builtin_clz(i + 1) - 1);
		if (TYPE == CHMIN) {
			if (a[i].max == a[i].min or g <= a[i].min) {
				tag<UPDATE>(i, g);
				return;
			}
			if (a[i].max != INT64_MIN) {
				a[i].sum -= a[i].max * a[i].max_count;
			}
			a[i].max = g;
			a[i].min_second = std::min(a[i].min_second, g);
			if (a[i].lazy_update != INT64_MAX) {
				a[i].lazy_update = std::min(a[i].lazy_update, g);
			}
			a[i].sum += g * a[i].max_count;
		} else if (TYPE == CHMAX) {
			if (a[i].max == a[i].min or a[i].max <= g) {
				tag<UPDATE>(i, g);
				return;
			}
			if (a[i].min != INT64_MAX) {
				a[i].sum -= a[i].min * a[i].min_count;
			}
			a[i].min = g;
			a[i].max_second = std::max(a[i].max_second, g);
			if (a[i].lazy_update != INT64_MAX) {
				a[i].lazy_update = std::max(a[i].lazy_update, g);
			}
			a[i].sum += g * a[i].min_count;
		} else if (TYPE == ADD) {
			if (a[i].max != INT64_MAX) {
				a[i].max += g;
			}
			if (a[i].max_second != INT64_MIN) {
				a[i].max_second += g;
			}
			if (a[i].min != INT64_MIN) {
				a[i].min += g;
			}
			if (a[i].min_second != INT64_MAX) {
				a[i].min_second += g;
			}
			a[i].lazy_add += g;
			if (a[i].lazy_update != INT64_MAX) {
				a[i].lazy_update += g;
			}
			a[i].sum += g * length;
		} else if (TYPE == UPDATE) {
			a[i].max = g;
			a[i].max_second = INT64_MIN;
			a[i].max_count = length;
			a[i].min = g;
			a[i].min_second = INT64_MAX;
			a[i].min_count = length;
			a[i].lazy_add = 0;
			a[i].lazy_update = INT64_MAX;
			a[i].sum = g * length;
		} else {
			assert (false);
		}
	}
	void pushdown(int i) {
		int l = 2 * i + 1;
		int r = 2 * i + 2;
		// update
		if (a[i].lazy_update != INT64_MAX) {
			tag<UPDATE>(l, a[i].lazy_update);
			tag<UPDATE>(r, a[i].lazy_update);
			a[i].lazy_update = INT64_MAX;
			return;
		}
		// add
		if (a[i].lazy_add != 0) {
			tag<ADD>(l, a[i].lazy_add);
			tag<ADD>(r, a[i].lazy_add);
			a[i].lazy_add = 0;
		}
		// chmin
		if (a[i].max < a[l].max) {
			tag<CHMIN>(l, a[i].max);
		}
		if (a[i].max < a[r].max) {
			tag<CHMIN>(r, a[i].max);
		}
		// chmax
		if (a[l].min < a[i].min) {
			tag<CHMAX>(l, a[i].min);
		}
		if (a[r].min < a[i].min) {
			tag<CHMAX>(r, a[i].min);
		}
	}
	void update(int i) {
		int l = 2 * i + 1;
		int r = 2 * i + 2;
		// chmin
		vector<ll> b { a[l].max, a[l].max_second, a[r].max, a[r].max_second };
		std::sort(b.rbegin(), b.rend());
		b.erase(std::unique(b.begin(), b.end()), b.end());
		a[i].max = b[0];
		a[i].max_second = b[1];
		a[i].max_count = (b[0] == a[l].max ? a[l].max_count : 0) + (b[0] == a[r].max ? a[r].max_count : 0);
		// chmax
		vector<ll> c { a[l].min, a[l].min_second, a[r].min, a[r].min_second };
		std::sort(c.begin(), c.end());
		c.erase(std::unique(c.begin(), c.end()), c.end());
		a[i].min = c[0];
		a[i].min_second = c[1];
		a[i].min_count = (c[0] == a[l].min ? a[l].min_count : 0) + (c[0] == a[r].min ? a[r].min_count : 0);
		// add
		a[i].lazy_add = 0;
		// update
		a[i].lazy_update = INT64_MAX;
		// sum
		a[i].sum = a[l].sum + a[r].sum;
	}

	template <char TYPE>
	ll range_get(int i, int il, int ir, int l, int r) {
		if (ir <= l or r <= il) {
			switch (TYPE) {
				case MIN: return INT64_MAX;
				case MAX: return INT64_MIN;
				case SUM: return 0;
				default: assert (false);
			}
		} else if (l <= il and ir <= r) {
			// base
			switch (TYPE) {
				case MIN: return a[i].min;
				case MAX: return a[i].max;
				case SUM: return a[i].sum;
				default: assert (false);
			}
		} else {
			pushdown(i);
			ll value_l = range_get<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r);
			ll value_r = range_get<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r);
			// mult
			switch (TYPE) {
				case MIN: return std::min(value_l, value_r);
				case MAX: return std::max(value_l, value_r);
				case SUM: return value_l + value_r;
				default: assert (false);
			}
		}
	}
};
ostream& operator<<(ostream& s, SegmentTreeBeats& seg) {
	for(ll i=0; i<seg.n; i++) s << seg.range_sum(i, i+1) << "\t\n"[i==seg.n-1];
	return s;
}


//区間add, 区間sum ができる
template< typename T >
class BinaryIndexedTree {
public:
	class BinaryIndexedTreeNaive {
	public:
		ll size;
		vector<T> data;
		BinaryIndexedTreeNaive(ll N) : size(N+1), data(size, 0) {}

		// sum [0, i)
		T sum(ll i) {
			T ret = 0;
			for(--i; i>=0; i=(i&(i+1))-1) ret += data[i];
			return ret;
		}

		// sum [i, j)
		T sum(ll i, ll j) {
			return sum(j) - sum(i);
		}

		// add x to i
		void add(ll i, T x) {
			for(; i < size; i|=i+1) data[i] += x;
		}
	};
	ll size;
	BinaryIndexedTreeNaive b,c;
	BinaryIndexedTree(ll N) : size(N), b(size), c(size) {}

	// add x to [i, j) 
	void add(ll i, ll j, T x) {
		if (i>=j) return;
		b.add(i,x*-i);
		b.add(j,x*j);
		c.add(i,x);
		c.add(j,x*-1);
	}

	// sum [0, i)
	T sum(ll i) { 
		return b.sum(i+1) + c.sum(i+1)*i;
	}

	// sum [i, j)
	T sum(ll i, ll j) {
		if (i>=j) return 0;
		return sum(j) - sum(i);
	}
};
template<typename T> std::ostream& operator<<(std::ostream& s, BinaryIndexedTree<T>& bit) { 
	for(ll i=0; i<bit.size; i++) s << bit.sum(i, i+1) << "\t\n"[i==bit.size-1];
	return s;
}

/*-----8<-----library-----8<-----*/

constexpr ll N_MAX = 2*1e5;

ll N, Q;

void solve() {
	cin >> N >> Q;


	ll ans = 0;
	vector<ll> tmpvec(N-2,N-2);
	SegmentTreeBeats segtate(all(tmpvec));
	SegmentTreeBeats segyoko(all(tmpvec));

	SegmentTreeBeats bit(N-2);//縦の石の数
	bit.range_add(0,N-2,N-2);

	while(Q--){
		ll type,x;
		cin>>type>>x;
		x-=2;
		if(type==1){
			ll t=segtate.range_sum(x,x+1);
			bit.range_add(x,x+1,-t);
			bit.range_chmax(x,x+1,0);

			segtate.range_chmin(x,x+1,0);
			segyoko.range_chmin(0,t,x);
			
		}else{
			ll t=segyoko.range_sum(x,x+1);
			bit.range_add(0,t,-1);
			bit.range_chmax(0,t,0);

			segtate.range_chmin(0,t,x);
		}

		debug(bit);
		debug(segtate);
		debug(segyoko);
	}


	ans=bit.range_sum(0,N-2);
	cout << ans << '\n';
}

// https://atcoder.jp/contests/abc179/tasks/abc179_f
signed main() {
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);
	//ll Q; cin >> Q; while(Q--)solve();
	solve();
	return 0;
}


Measured memory_bytes: 115424