#include <bits/stdc++.h>
using namespace std;
int status[100001];
int main() {
  ios::sync_with_stdio(false);
  string str;
  int n, m, l, r, min1 = 100001, max2 = -1;
  unsigned long long ans;
  cin >> n >> m;
  if (!m) {
    cout << n - 1 << endl;
    return 0;
  }
  for (int i = 0; i < m; ++i) {
    cin >> l >> r;
    if (l > r) swap(l, r);
    if (status[l] == 1 || status[r] == 2) {
      cout << 0 << endl;
      return 0;
    }
    status[l] = 2;
    status[r] = 1;
    min1 = min(min1, r);
    max2 = max(max2, l);
  }
  if (max2 >= min1)
    ans = 0;
  else {
    ans = min1 - max2;
  }
  cout << ans << endl;
}
