#include <bits/stdc++.h>
int main() {
  int n, m, i, a, b, x;
  scanf("%d %d", &n, &m);
  int max1 = 0, max2 = 0, min2 = n;
  for (i = 0; i < m; i++) {
    scanf("%d %d", &a, &b);
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    if (a > max2) max2 = a;
    if (a < min2) min2 = a;
    if (b > max1) max1 = b;
  }
  if (max1 >= min2)
    printf("0\n");
  else if (m == 0)
    printf("%d\n", n - 1);
  else {
    int num = min2 - max1 - 1;
    num = num + 1;
    printf("%d\n", num);
  }
  return 0;
}
