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