#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, u = 0, v = 100001;
  cin >> n >> m;
  if (m == 0) {
    cout << n - 1;
    return 0;
  }
  pair<int, int> arr[m];
  for (int i = 0; i < m; ++i) {
    cin >> arr[i].first >> arr[i].second;
    if (arr[i].second < arr[i].first) swap(arr[i].first, arr[i].second);
    if (u < arr[i].first) u = arr[i].first;
    if (v > arr[i].second) v = arr[i].second;
  }
  if (v <= u) {
    cout << "0";
    return 0;
  }
  cout << v - u;
}
