#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
int i, j, k, n, m, l, x, y, z, ans;
int main() {
  scanf("%d%d", &n, &m);
  k = 2147483647;
  l = 0;
  while (m--) {
    scanf("%d%d", &x, &y);
    if (x > y) swap(x, y);
    if (x > l) l = x;
    if (y < k) k = y;
  }
  k = k - l;
  if (m == 0)
    k = n - 1;
  else if (k < 0)
    k = 0;
  if (k > n - 1) k = n - 1;
  cout << k << endl;
}
