#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} int main(){ int h, w; cin >> h >> w; vvi a(h, vi(h + w)); rep(i, w){ int x, y; cin >> x >> y; x--; y--; a[x][y]++; } int cnt = 0; long long ans = 0; rep(y, h + w) for(int x = h - 1; x >= 0; x--){ if(a[x][y] > 0){ long long dist = 0; if(y >= cnt and y <= cnt + x) dist = x; else dist = x + min(abs(y - cnt), abs(y - (cnt + x))); ans += dist; cnt++; } } cout << ans << "\n"; return 0; }