#include <bits/stdc++.h>
using namespace std;
int v[5009];
int a[5009];
int main() {
  int n;
  int m;
  cin >> n;
  cin >> m;
  set<int> s1, s2;
  bool flag = false;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    int a = min(x, y);
    int b = max(x, y);
    if ((s1.find(b) != s1.end()) || (s2.find(a) != s2.end())) {
      flag = true;
      continue;
    } else if (!s2.empty() && *(s2.begin()) < a) {
      flag = true;
      continue;
    } else if (!s1.empty() && *(s1.rbegin()) > b) {
      flag = true;
      continue;
    }
    s1.insert(a);
    s2.insert(b);
  }
  if (m == 0)
    cout << n - 1 << endl;
  else if (flag)
    cout << "0" << endl;
  else {
    int count = *s2.begin() - (*s1.rbegin());
    cout << count << endl;
  }
  return 0;
}
