#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int max2 = 1, min1 = n;
  for (int i = 0; i < m; i++) {
    int x1, x2;
    cin >> x1 >> x2;
    if (x2 > x1) swap(x1, x2);
    if (min1 > x1) min1 = x1;
    if (max2 < x2) max2 = x2;
  }
  if (min1 <= max2) {
    cout << 0;
    return 0;
  } else {
    int k = min1 - max2;
    cout << k;
  }
  return 0;
}
