#include <bits/stdc++.h>
using namespace std;
int u[100005], v[100005];
int div1[100005], div2[100005];
int main() {
  int n, m, i;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d %d", &u[i], &v[i]);
    if (u[i] > v[i]) swap(u[i], v[i]);
    div2[u[i]] = 1;
    div1[v[i]] = 1;
  }
  if (m == 0) {
    cout << n - 1;
    return 0;
  }
  for (i = 1; i <= n; i++) {
    if (div1[i] == 1 && div2[i] == 1) {
      cout << "0";
      return 0;
    }
  }
  int large, small;
  for (i = n; i >= 1; i--) {
    if (div2[i] == 1) {
      large = i;
      break;
    }
  }
  for (i = 1; i <= n; i++) {
    if (div1[i] == 1) {
      small = i;
      break;
    }
  }
  if (small < large)
    cout << 0;
  else
    cout << small - large;
  return 0;
}
