#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m, x, y, mn, mx;
  cin >> n >> m;
  mn = n;
  mx = 1;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y;
    if (x > y) swap(x, y);
    mn = min(mn, y);
    mx = max(mx, x);
  }
  cout << max(0, mn - mx) << endl;
  return 0;
}
