#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n, x, y, min, max = 1;
  cin >> n >> m;
  min = n;
  while (m--) {
    cin >> x >> y;
    if (x > y) swap(x, y);
    if (x > max) max = x;
    if (y < min) min = y;
  }
  if (min - max > 0)
    cout << min - max;
  else
    cout << 0;
  return 0;
}
