#include using namespace std; signed main() { ios::sync_with_stdio(false); int64_t D; int Q; cin >> D >> Q; vector A(Q), B(Q); for (int i = 0; i < Q; ++i) { cin >> A[i] >> B[i]; } vector pnts; map mp; for (int i = 0; i < Q; ++i) { pnts.emplace_back(A[i]); pnts.emplace_back(B[i] + 1); } sort(pnts.begin(), pnts.end()); pnts.erase(unique(pnts.begin(), pnts.end()), pnts.end()); for (int i = 0; i < pnts.size(); ++i) { mp[pnts[i]] = i; } int BS = sqrt(pnts.size()); vector cnt(pnts.size()), bcov((pnts.size() + BS - 1) / BS), bcnt((pnts.size() + BS - 1) / BS); for (int qi = 0; qi < Q; ++qi) { int x = mp[A[qi]]; int y = mp[B[qi] + 1]; while (x % BS > 0 && x < y) { if (cnt[x] == 0) { cnt[x] = 1; if (++bcnt[x / BS] == BS) { bcov[x / BS] = 1; } } ++x; } while (x / BS != y / BS) bcov[x++ / BS] = 1; while (x < y) { if (cnt[x] == 0) { cnt[x] = 1; if (++bcnt[x / BS] == BS) { bcov[x / BS] = 1; } } ++x; } int64_t ans = 0; for (int i = 0; i < pnts.size(); ++i) { if (cnt[i] || bcov[i / BS]) { int j = i + 1; while (j < pnts.size()) { if (bcov[j / BS]) { j = (j + BS) / BS * BS; } else if (cnt[j]) { ++j; } else { break; } } if (j >= pnts.size()) { ans = max(ans, D - pnts[i]); } else { ans = max(ans, pnts[j] - pnts[i]); } i = j; } else if (bcnt[i / BS] == 0) { i = (i + BS) / BS * BS - 1; } } cout << ans << endl; } return 0; }