#include <bits/stdc++.h>
using namespace std;
int n, m;
int mx = 1e9, mn = 0;
int u, v;
int main() {
  scanf("%d %d", &n, &m);
  mx = n;
  mn = 1;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);
    mx = min(mx, max(u, v));
    mn = max(mn, min(u, v));
  }
  if (mx - mn <= 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << mx - mn << endl;
}
