#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  int m = 1;
  for (int i = 0; i < m; i++) {
    solve();
  }
  return 0;
}
long long INF = INT64_MAX;
long long mod = 1000000000LL;
vector<vector<long long> > g;
vector<long long> used;
set<long long> cur;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long _max = n, _min = 1;
  for (long long i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v;
    long long u1 = max(u, v);
    long long v1 = min(u, v);
    _max = min(_max, u1);
    _min = max(_min, v1);
  }
  cout << max(0LL, _max - _min);
}
