#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int INF = 1e9;
const double EPS = 1e-8;
int main() {
  int n, m;
  scanf("%d", &(n)), scanf("%d", &(m));
  int l = 0, r = INF;
  if (m == 0)
    cout << n - 1 << endl;
  else {
    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%d", &(x)), scanf("%d", &(y));
      if (y < x) swap(x, y);
      l = max(l, x);
      r = min(r, y);
    }
    if (r - l > 0)
      cout << r - l << endl;
    else
      cout << "0" << endl;
  }
}
