#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
  int n, m, u, v;
  int low, high;
  int i;
  scanf("%d %d", &n, &m);
  low = 1;
  high = n;
  for (i = 0; i < m && scanf("%d %d", &u, &v) == 2; i++) {
    int l = (((u) > (v)) ? (v) : (u)), h = (((u) < (v)) ? (v) : (u));
    low = (((low) < (l)) ? (l) : (low));
    high = (((high) > (h)) ? (h) : (high));
  }
  printf("%d\n", (low < high) ? high - low : 0);
  return 0;
}
