#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)5e5 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int L = n;
  int R = 1;
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    if (from > to) {
      swap(from, to);
    }
    L = min(L, to - 1);
    R = max(R, from + 1);
  }
  int ans = 0;
  for (int i = 1; i <= min(L, n - 1); i++) {
    int right = i + 1;
    if (right >= R) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
