#include <bits/stdc++.h>
using namespace std;
int f[5007], mx[5007];
int main(void) {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; ++i) f[i] = i + 1;
  for (int j = q; j--;) {
    int l, r;
    scanf("%d %d", &l, &r);
    for (int i = l; i <= r; ++i) {
      if (f[i] > l) f[i] = l;
    }
  }
  for (int m = q - 2; m--;) {
    for (int i = n + 1; --i;) mx[i] = max(mx[i], mx[f[i] - 1] + i - f[i] + 1);
    for (int i = 1; i <= n; ++i)
      if (mx[i] < mx[i - 1]) mx[i] = mx[i - 1];
  }
  printf("%d\n", mx[n]);
  return 0;
}
