#include <bits/stdc++.h>
inline int F() {
  register int aa, bb, ch;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  ch == '-' ? aa = bb = 0 : (aa = ch - '0', bb = 1);
  while (ch = getchar(), ch >= '0' && ch <= '9') aa = aa * 10 + ch - '0';
  return bb ? aa : -aa;
}
int main() {
  int n = F(), m = F();
  int mn = 1, mx = n;
  for (int i = 1; i <= m; ++i) {
    int a = F(), b = F();
    if (a > b) a ^= b ^= a ^= b;
    if (a > mn) mn = a;
    if (b < mx) mx = b;
  }
  printf("%d\n", (mx - mn) < 0 ? 0 : (mx - mn));
}
