#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 5;
const int mod = (int)1e9 + 7;
const int MAX = (int)1e5 + 5;
void build() { return; }
int n, m;
int typ[MAX];
pair<int, int> store[MAX];
void Init() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < (m); ++i)
    scanf("%d %d", &store[i].first, &store[i].second);
  return;
}
void sol() {
  for (int i = 0; i < (m); ++i) {
    int u = store[i].first, v = store[i].second;
    if (u > v) swap(u, v);
    if (typ[v] == 1 || typ[u] == 2) {
      puts("0");
      return;
    } else {
      typ[u] = 1;
      typ[v] = 2;
    }
  }
  if (m == 0) {
    printf("%d", n - 1);
    return;
  }
  int s, e;
  for (int i = (1); i < (n + 1); ++i)
    if (typ[i] == 2) {
      e = i;
      break;
    }
  for (int i = n; i >= 1; --i)
    if (typ[i] == 1) {
      s = i;
      break;
    }
  if (s > e) {
    puts("0");
    return;
  }
  int ans = 0;
  for (int i = (s); i < (e); ++i)
    if (!typ[i]) ans++;
  ans++;
  cout << ans << endl;
  return;
}
int main(void) {
  Init();
  build();
  sol();
  return 0;
}
