#include <bits/stdc++.h>
int main(void) {
  int n, m;
  int l, r;
  scanf("%d %d", &n, &m);
  l = 1;
  r = n;
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    l = (((l) > ((((u) < (v)) ? (u) : (v)))) ? (l)
                                             : ((((u) < (v)) ? (u) : (v))));
    r = (((r) < ((((u) > (v)) ? (u) : (v)))) ? (r)
                                             : ((((u) > (v)) ? (u) : (v))));
  }
  printf("%d\n", (((r - l) > (0)) ? (r - l) : (0)));
  return 0;
}
