#include using namespace std; using ll = long long; using pll = pair; template bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; } int main(){ ll n, m; cin >> n >> m; vector edge(m); vector x(m * 2); for(ll i = 0; i < m; i++){ ll a, b; cin >> a >> b; a--; b--; edge[i] = {a, b}; x[i * 2] = a; x[i * 2 + 1] = b; } sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); vector g(x.size(), vector{}); for(auto& [a, b] : edge){ a = lower_bound(x.begin(), x.end(), a) - x.begin(); b = lower_bound(x.begin(), x.end(), b) - x.begin(); g[b].push_back(a); } vector mx = x; priority_queue q; for(ll i = 0; i < x.size(); i++) q.emplace(x[i], i); while(q.size()){ auto [c, at] = q.top(); q.pop(); if(mx[at] != c) continue; for(ll i : g[at]) if(chmax(mx[i], c)) q.emplace(c, i); } ll ans = n * (n + 1) / 2; for(ll i = 0; i < x.size(); i++) ans += mx[i] - x[i]; cout << ans << endl; }