#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
void sync_stdio() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
}
struct Sync_stdio {
  Sync_stdio() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
  }
} _sync_stdio;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > v(m);
  for (int i = (0); i < (m); ++i) {
    cin >> v[i].first >> v[i].second;
    if (v[i].first > v[i].second) {
      swap(v[i].first, v[i].second);
    }
  }
  int minc = n;
  int maxc = 1;
  for (int i = (0); i < (m); ++i) {
    minc = min(minc, v[i].second);
    maxc = max(maxc, v[i].first);
  }
  cout << max(0, minc - maxc);
  return 0;
}
