#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  int ans;
  int a, b, x, y, maxx = 1, minn = n;
  int temp = m;
  for (int i = 0; i < m; i++) {
    scanf("%d", &a);
    scanf("%d", &b);
    x = min(a, b);
    y = max(a, b);
    maxx = max(maxx, x);
    minn = min(minn, y);
  }
  ans = (minn - maxx);
  if (ans < 0) {
    cout << 0;
    return;
  }
  cout << ans;
}
int main() {
  solve();
  return 0;
}
