#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int n, t;

void solve() {
    cin >> n >> t;
    map<int, int> D;
    D[1] = D[n + 1] = 0;
    while (t--) {
        int l, r; cin >> l >> r;
        D[l]++; D[r + 1]--;        
    }
    ll ans = 0;
    int cur = 0;
    for (auto it = D.begin(); it != D.end(); it++) {
        int pos = it->first;
        cur += it->second;
        if (pos > n) break;
        auto nxt_it = next(it);
        if (nxt_it != D.end()) {
            int nxt = nxt_it->first;
            int cnt = min(nxt - 1, n) - pos + 1;
            if (cnt > 0 && cur % 3 == 0) ans += cnt;
        }
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define TASK "LOCK"
    if (fopen(TASK".INP", "r")) {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    
    int tests = 1; // cin >> tests;
    while (tests--) solve();

    #ifdef LOCAL
    cerr << "\nTime elapsed: " << clock() << " ms.\n";
    #endif
    return 0;
};
