#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, m, u, v, mmax = 0, mmin = 999999999;
  scanf("%ld%ld", &n, &m);
  if (!m)
    printf("%ld", n - 1);
  else {
    while (m--) {
      scanf("%ld%ld", &u, &v);
      if (u > v) swap(u, v);
      mmax = max(mmax, u);
      mmin = min(mmin, v);
    }
    printf("%ld", max(0L, mmin - mmax));
  }
}
