#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x, y;
  scanf("%d%d", &n, &m);
  int maxx = -1, maxy = n;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (x > y) swap(x, y);
    maxx = max(maxx, x);
    maxy = min(maxy, y);
  }
  if (m == 0)
    printf("%d\n", n - 1);
  else if (maxx >= maxy)
    printf("0\n");
  else
    printf("%d\n", maxy - maxx);
  return 0;
}
