#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  long long int n, m, i, j, x, y, l = INF, u = 0;
  cin >> n >> m;
  if (m == 0)
    cout << n - 1 << endl;
  else {
    for (i = 0; i < m; i++) {
      cin >> x >> y;
      if (y < x) swap(x, y);
      u = max(u, x);
      l = min(l, y);
    }
    if (l - u > 0)
      cout << l - u << endl;
    else
      cout << 0 << endl;
  }
}
