#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Node {
  int x;
  long long cnt;
};
bool vis[MAXN];
int dp[MAXN];
int N, K;
int l, r;
vector<int> dir;
long long cnt = 0;
void BFS() {
  queue<Node> Q;
  Node now, next;
  now.x = 1;
  now.cnt = 0;
  Q.push(now);
  vis[now.x] = true;
  long long mx = 0, mn = INF;
  while (!Q.empty()) {
    now = Q.front(), Q.pop();
    for (int i = 0; i < dir.size(); i++) {
      next.x = now.x + dir[i];
      if (next.x <= N) {
        if (vis[next.x]) {
          dp[next.x]++;
          if (dp[next.x] > mx) mx = dp[next.x];
          if (dp[next.x] < mn) mn = dp[next.x];
        } else {
          vis[next.x] = true;
        }
        Q.push(next);
      }
    }
  }
  cout << mx + mn;
}
int main(int argc, char *argv[]) {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < K; i++) {
    scanf("%d %d", &l, &r);
    for (int i = l; i <= r; i++) dir.push_back(i);
  }
  sort(dir.begin(), dir.begin() + dir.size());
  BFS();
  return 0;
}
