#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int inf = 0x3f3f3f3f;
priority_queue<int, vector<int>, greater<int> > q;
int L[5005], dp[5005];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++) {
      L[i] = i + 1;
      dp[i] = 0;
    }
    for (int i = 1; i <= m; i++) {
      int l, r;
      scanf("%d%d", &l, &r);
      for (int j = l; j <= r; j++) {
        L[j] = min(L[j], l);
      }
    }
    for (int k = 1; k <= m - 2; k++) {
      for (int i = n; i >= 1; i--) {
        dp[i] = max(dp[L[i] - 1] + i - L[i] + 1, dp[i]);
      }
      for (int i = 1; i <= n; i++) {
        dp[i] = max(dp[i], dp[i - 1]);
      }
    }
    printf("%d\n", dp[n]);
  }
  return 0;
}
