#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> nums(n, 0);
  bool zero = false;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b) {
      swap(a, b);
    }
    if (nums[a] == -1 || nums[b] == 1) {
      cout << 0 << endl;
      return 0;
    }
    nums[a] = 1;
    nums[b] = -1;
  }
  int posFirst = -1;
  int posSecond = -1;
  for (int i = 0; i < n; i++) {
    if (nums[i] == 1) {
      posFirst = i;
    }
    if (posSecond == -1 && nums[i] == -1) {
      posSecond = i;
    }
  }
  if (posFirst != -1) {
    if (posSecond - posFirst > 0) {
      cout << (posSecond - posFirst) << endl;
    } else {
      cout << 0 << endl;
    }
  } else {
    cout << n - 1 << endl;
  }
  return 0;
}
