#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> firstdiv;
  vector<int> seconddiv;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    int f, s;
    f = min(a, b);
    s = max(a, b);
    firstdiv.push_back(f);
    seconddiv.push_back(s);
  }
  if (m == 0) {
    int ans = n - 1;
    printf("%d", ans);
    return 0;
  }
  sort(firstdiv.begin(), firstdiv.end());
  sort(seconddiv.begin(), seconddiv.end());
  if (firstdiv[firstdiv.size() - 1] >= seconddiv[0])
    printf("0");
  else {
    int ans = seconddiv[0] - firstdiv[firstdiv.size() - 1];
    printf("%d", ans);
  }
  return 0;
}
