#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << n - 1 << "\n";
    exit(0);
  }
  vector<pair<long long, long long> > v;
  for (long long i = (long long)(1); i <= (long long)(m); i++) {
    long long x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    v.push_back({x, y});
  }
  sort(v.begin(), v.end());
  long long val = v.back().first;
  long long mn = n;
  for (auto it : v) {
    mn = min(mn, it.second);
  }
  if (mn < val) {
    cout << 0 << "\n";
    exit(0);
  }
  cout << mn - val << "\n";
}
