#include <bits/stdc++.h>
using namespace std;
const int nmax = 110000;
int n, m;
int a[nmax], b[nmax];
int main() {
  int i;
  scanf("%d%d", &n, &m);
  a[0] = 1;
  b[0] = n;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", a + i, b + i);
    if (a[i] > b[i]) swap(a[i], b[i]);
  }
  sort(a, a + m + 1);
  sort(b, b + m + 1);
  if (b[0] - a[m] < 0)
    printf("0\n");
  else
    printf("%d\n", b[0] - a[m]);
  return 0;
}
