#include <bits/stdc++.h>
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF) {
    int min = n, max = 0;
    while (m--) {
      int x, y;
      scanf("%d %d", &x, &y);
      int t = x > y ? x : y;
      min = min > t ? t : min;
      t = x < y ? x : y;
      max = max < t ? t : max;
    }
    if (min <= max) {
      printf("0\n");
      continue;
    }
    printf("%d\n", min - max == n ? n - 1 : min - max);
  }
  return 0;
}
