#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long mod = 1e9 + 9;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, m, a = 0, b = INF;
  cin >> n >> m;
  if (m == 0) {
    cout << n - 1 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    a = max(a, x);
    b = min(b, y);
  }
  cout << max(0, b - a) << endl;
  return 0;
}
