#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  bool ch = 0;
  bool arr[n + 1];
  vector<int> vec, vec1;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (b > a) swap(a, b);
    vec.push_back(a);
    vec1.push_back(b);
  }
  if (!m)
    cout << n - 1;
  else {
    sort(vec.begin(), vec.end());
    sort(vec1.begin(), vec1.end());
    cout << max(0, vec[0] - vec1[vec1.size() - 1]);
  }
  return 0;
}
