#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::queue;
using std::sort;
using std::string;
using std::vector;
int main() {
  int n, m;
  cin >> n >> m;
  int answer = 0;
  int a, b, temp;
  int left = -1, right = n + 5;
  if (m == 0) {
    cout << n - 1;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    if (a > b) {
      temp = a;
      a = b;
      b = temp;
    }
    if (a > left) {
      left = a;
    }
    if (b < right) {
      right = b;
    }
  }
  if (left >= right || left == n || right == 1) {
    cout << 0;
    return 0;
  } else {
    cout << right - left;
  }
  return 0;
}
