#include <bits/stdc++.h>   using namespace std;   template <class T>   void mini(T& a, T b) {     a = min(a, b);   }   template <class T>   void maxi(T& a, T b) {     a = max(a, b);   }   template <class T1, class T2>   ostream& operator<<(ostream& out, pair<T1, T2> a) {     return out << a.first <<     << a.second;   }   template <class T>   ostream& operator<<(ostream& out, vector<T> a) {     for (auto& i : a) out << i <<    ;     return out;   }   template <class T>   ostream& operator<<(ostream& out, set<T> a) {     for (auto& i : a) out << i <<    ;     return out;   }   template <class T>   ostream& operator<<(ostream& out, multiset<T> a) {     for (auto& i : a) out << i <<    ;     return out;   }   template <class T1, class T2>   ostream& operator<<(ostream& out, map<T1, T2> a) {     for (auto& i : a) out << i <<   n ;     return out;   }   template <class T1, class T2>   ostream& operator<<(ostream& out, unordered_map<T1, T2> a) {     for (auto& i : a) out << i <<   n ;     return out;   }   template <class T>   ostream& operator<<(ostream& out, queue<T> a) {     while (!a.empty()) {       cout << a.front() <<    ;       a.pop();     }     return out;   }   template <class T>   ostream& operator<<(ostream& out, deque<T> a) {     while (!a.empty()) {       cout << a.front() <<    ;       a.pop_front();     }     return out;   }   template <class T>   ostream& operator<<(ostream& out, priority_queue<T> a) {     while (!a.empty()) {       cout << a.top() <<   n ;       a.pop();     }     return out;   }   template <class T>   void out(T a) {     cout << a << endl;   }   template <class T1, class T2>   void out(T1 a, T2 b) {     cout << a <<     << b << endl;   }   template <class T1, class T2, class T3>   void out(T1 a, T2 b, T3 c) {     cout << a <<     << b <<     << c << endl;   }   void out() { cout <<  OK  << endl; }   template <class T>   void sort(vector<T>& a) {     sort(a.begin(), a.end());   }   template <class T>   void uniq(vector<T>& a) {     sort(a);     a.erase(unique(a.begin(), a.end()), a.end());   }   template <class T>   vector<T> set_int(vector<T> a, vector<T> b) {     sort(a);     sort(b);     vector<T> res;     set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));     return res;   }   istream& operator>>(istream& in, __int128& a) {     int64_t b;     cin >> b;     a = b;     return in;   }   ostream& operator<<(ostream& out, const __int128 a) {     unsigned __int128 b = a < 0 ? -a : a;     char buf[128];     char* c = end(buf);     do {       --c;       *c =  0123456789 [b % 10];       b /= 10;     } while (b);     if (a < 0) {       --c;       *c =  - ;     }     int len = end(buf) - c;     out.rdbuf()->sputn(c, len);     return out;   }   const int N = 1e5 + 5;   const int inf = 1e9;   vector<int> dp, pr;   vector<bool> used;   vector<int> a[N];   void Solve() {     int n, k;     cin >> n >> k;     int h = 5 * sqrt(k);     int ans = 0;     for (int i = 1; i <= k; i++) {       int r, c;       cin >> r >> c;       if (n - r < h)         a[c - 1].push_back(n - r);       else         ans += 3;     }     dp.resize(h, 0);     pr.resize(h, 0);     used.resize(h, 0);     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j <= h - 1; j++) used[j] = 0;       for (int j : a[i]) used[j] = 1;       for (int& j : dp) j = inf;       int suf = 0;       for (int j = h - 1; j >= 0; j--) {         if (used[j]) suf += 3;         int cur = suf + pr[max(j - 1, 0)];         mini(dp[j], cur);         if (j) cur += 2 + j * (j + 1) / 2;         mini(dp[0], cur);       }       for (int j = 0; j <= h - 2; j++) mini(dp[j + 1], dp[j]);       swap(dp, pr);     }     ans += pr[0];     out(ans);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     Solve();     return 0;   }