#include <bits/stdc++.h>
int main() {
  long long int i, j = 0, k = 0, n, m, a, b, ans, nope = 0, cn = 0, cn1 = 0,
                   cn2 = 0;
  scanf("%lld %lld", &n, &m);
  long long int bi, sm, m_2 = 0, m_1 = 1000000000;
  long long int pos[n + 1];
  for (i = 0; i < m; i++) {
    scanf("%lld %lld", &a, &b);
    if (a > b) {
      bi = a;
      sm = b;
    } else {
      bi = b;
      sm = a;
    }
    pos[bi] = 1;
    pos[sm] = 2;
    if (bi < m_1) m_1 = bi;
    if (sm > m_2) m_2 = sm;
  }
  if (m_2 > m_1 || m_2 == m_1)
    printf("0\n");
  else {
    ans = m_1 - m_2;
    if (ans > n) ans = n - 1;
    printf("%lld\n", ans);
  }
  return 0;
}
