#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int main(void) {
  int n, m;
  cin >> n >> m;
  int lo = 1;
  int hi = n - 1;
  for (int i = (int)(0); i < (int)(m); i++) {
    int u, v;
    cin >> u >> v;
    if (u > v) {
      swap(u, v);
    }
    lo = max(lo, u);
    hi = min(hi, v - 1);
  }
  cout << (lo <= hi ? hi - lo + 1 : 0) << endl;
}
