#include <bits/stdc++.h>
using namespace std;
set<int> l, r;
long long n, m, a, b;
int x, y;
int main() {
  cin >> n >> m;
  if (m == 0) {
    cout << n - 1 << endl;
    return 0;
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    l.insert(min(a, b));
    r.insert(max(a, b));
  }
  x = *(l.rbegin());
  y = *(r.begin());
  cout << max(0, y - x) << endl;
  return 0;
}
