#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double PI = 3.141592653589793238463;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int x, m;
  cin >> x >> m;
  int div1 = x, div2 = 1;
  int r, l;
  for (int i = 0; i < m; i++) {
    cin >> r >> l;
    if (r > l) swap(r, l);
    div2 = max(div2, r);
    div1 = min(div1, l);
  }
  cout << (div1 - div2 <= 0 ? 0 : div1 - div2) << '\n';
  return 0;
}
