#include <bits/stdc++.h>
using namespace std;
int k, n, dp[200005], vis[200005], use[200005], tot;
long long mod = 998244353;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = a; j <= b; j++)
      if (vis[j] == 0) vis[j] = 1, use[++tot] = j;
  }
  dp[1] = 1;
  for (int i = 1; i <= tot; i++) dp[1 + use[tot]] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= tot; j++) {
      if (dp[i - use[j]] != 0 && dp[use[j]] != 0) dp[i]++;
    }
  }
  cout << dp[n] << endl;
}
