#include <bits/stdc++.h>
int main() {
  std::ios::sync_with_stdio(false);
  char q[100001] = {0};
  int n, m, p1, p2, count = 0, last1 = 1, first2 = 100000;
  std::cin >> n >> m;
  if (m == 0) {
    std::cout << n - 1;
    return 0;
  }
  for (int i = 0; i < m; ++i) {
    std::cin >> p1 >> p2;
    if (p1 > p2) {
      int t = p1;
      p1 = p2;
      p2 = t;
    }
    if (q[p1] == 2 || q[p2] == 1 || p1 > first2 || p2 < last1) {
      std::cout << "0";
      return 0;
    }
    q[p1] = 1;
    q[p2] = 2;
    if (last1 < p1) last1 = p1;
    if (first2 > p2) first2 = p2;
  }
  count = first2 - last1 - 1;
  if (q[last1 + 1] == 0) count++;
  if (count == 0) count = 1;
  std::cout << count;
  return 0;
}
