#include <bits/stdc++.h>
using namespace std;
int main() {
  int nmin, nmax = 1;
  int n, m, a, b;
  scanf("%d%d", &n, &m);
  nmin = n;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    if (b < a) swap(a, b);
    nmin = min(nmin, b);
    nmax = max(nmax, a);
  }
  if (nmax < nmin)
    printf("%d\n", nmin - nmax);
  else
    printf("0\n");
  return 0;
}
