#include <bits/stdc++.h>
int a[100005];
int min(int x, int y) {
  if (x > y)
    return y;
  else
    return x;
}
int main() {
  int n, m, p, q, flag[3], f = 0, answer, pos2 = INT_MIN, pos1 = INT_MAX;
  flag[1] = flag[2] = 0;
  flag[0] = n;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &p, &q);
    int small = min(p, q);
    if (pos2 < small) pos2 = small;
    int max = p + q - small;
    if (pos1 > max) pos1 = max;
    if (pos2 > pos1) f = 1;
    if (a[small] != 1) {
      a[small] = 2;
      flag[2]++;
      flag[0]--;
    } else
      f = 1;
    if (a[max] != 2) {
      a[max] = 1;
      flag[1]++;
      flag[0]--;
    } else
      f = 1;
  }
  if (f == 1)
    answer = 0;
  else {
    if (flag[1] != 0)
      answer = (pos1 - pos2 - 1) + 1;
    else
      answer = n - 1;
  }
  printf("%d\n", answer);
  return 0;
}
