#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int first_div = 1;
  int second_div = n;
  while (m-- > 0) {
    int a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    first_div = max(a, first_div);
    second_div = min(b, second_div);
  }
  if (first_div >= second_div)
    cout << 0;
  else
    cout << second_div - first_div;
  return 0;
}
