#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
  int n, m;
  cin >> n >> m;
  set<int> first;
  set<int> second;
  first.clear();
  second.clear();
  if (m == 0) {
    cout << n - 1;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x > y) swap(x, y);
    if (first.find(-x) != first.end()) {
      if (first.find(-y) != first.end()) {
        cout << 0;
        return 0;
      }
    }
    if (second.find(x) != second.end()) {
      if (second.find(y) != second.end()) {
        cout << 0;
        return 0;
      }
    }
    if (first.find(-y) != first.end()) {
      cout << 0;
      return 0;
    }
    if (second.find(x) != second.end()) {
      cout << 0;
      return 0;
    }
    if (second.find(y) != second.end()) {
      first.insert(-x);
      continue;
    }
    first.insert(-x);
    second.insert(y);
  }
  cout << max(0, *second.begin() + *first.begin());
  return 0;
}
