#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, mi, ma, x, y;
  cin >> n >> m;
  mi = 1;
  ma = n;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    mi = max(mi, min(x, y));
    ma = min(ma, max(x, y));
  }
  cout << max(ma - mi, 0);
}
