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