#include <bits/stdc++.h>
using namespace std;
int n, m, in[100005], out[100005], sumIn[100005], sumOut[100005];
struct Node {
  int u, v;
} pgc[100005];
int main() {
  int u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    if (u > v) swap(u, v);
    in[v]++, out[u]++;
  }
  for (int i = 1; i <= n; i++) sumIn[i] = sumIn[i - 1] + in[i];
  for (int i = n; i; i--) sumOut[i] = sumOut[i + 1] + out[i];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (sumIn[i] == 0 && sumOut[i + 1] == 0) ans++;
  }
  cout << ans << endl;
  return 0;
}
