#include <bits/stdc++.h>
using namespace std;
set<long long> two, one;
set<long long>::iterator it1;
set<long long>::reverse_iterator rit;
int main() {
  long long i, j, k, l, m, n, t, ans, x, y, temp;
  int flag = 0;
  scanf("%lld %lld ", &n, &m);
  if (m == 0) {
    printf("%lld\n", n - 1);
    return 0;
  }
  int insertion = 0;
  for (i = 0; i < m; i++) {
    scanf("%lld %lld", &x, &y);
    if (flag == 1) continue;
    if (y < x) {
      temp = x;
      x = y;
      y = temp;
    }
    rit = one.rbegin();
    it1 = two.begin();
    if (insertion == 1 && y < (*rit)) {
      flag = 1;
    } else if (insertion == 1 && x > (*it1)) {
      flag = 1;
    } else {
      one.insert(x);
      two.insert(y);
      insertion = 1;
    }
  }
  it1 = two.begin();
  long long secini = *it1;
  int sizeone = one.size();
  long long oneend;
  rit = one.rbegin();
  oneend = *rit;
  if (flag == 1) {
    printf("0\n");
  } else
    printf("%lld\n", secini - oneend);
  return 0;
}
