#include <bits/stdc++.h>
using namespace std;
int n, m, a[2000000], b[2000000];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    if (u > v) swap(u, v);
    a[v]++, b[u]++;
  }
  for (int i = 1; i <= n; i++) a[i] += a[i - 1];
  for (int i = n; i >= 1; i--) b[i] += b[i + 1];
  int co = 0;
  for (int i = 1; i < n; i++)
    if (!a[i] and !b[i + 1]) co++;
  cout << co;
}
