#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  set<int> s1, s2, med;
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int first, second;
    cin >> first >> second;
    if (first > second) swap(first, second);
    s1.insert(first);
    s2.insert(second);
  }
  if (m == 0) {
    cout << n - 1;
    return 0;
  }
  set<int>::iterator it = s1.end();
  it--;
  if ((*it) > (*(s2.begin()))) {
    cout << 0;
    return 0;
  }
  int lim1 = *it, lim2 = *(s2.begin());
  cout << lim2 - lim1;
  return 0;
}
