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