#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
pair<int, int> pr[maxn];
int n, m;
int v1[maxn], v2[maxn], v3[maxn];
int m1, m2;
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x > y) swap(x, y);
    pr[i].first = x, pr[i].second = y;
  }
  if (m == 0) {
    n -= 2;
    if (n == 0)
      cout << 1 << endl;
    else {
      long long mm = n;
      cout << mm + 1 << endl;
    }
    return 0;
  }
  int flag = 0;
  m1 = 0, m2 = 0x3f3f3f3f;
  for (int i = 1; i <= m; i++) {
    if (v1[pr[i].second] || v2[pr[i].first]) {
      flag = 1;
      break;
    }
    v1[pr[i].first] = v2[pr[i].second] = 1;
    v3[pr[i].first] = v3[pr[i].second] = 1;
    m1 = max(pr[i].first, m1), m2 = min(pr[i].second, m2);
  }
  if (flag || m1 >= m2) {
    printf("0\n");
  } else {
    long long cnt = 0;
    for (int i = 1; i <= n; i++)
      if (!v3[i]) {
        if (i > m1 && i < m2) cnt++;
      }
    if (cnt == 0)
      cout << 1 << endl;
    else
      cout << cnt + 1 << endl;
  }
  return 0;
}
