#include <bits/stdc++.h>
using namespace std;
int divs[100002] = {
    0,
};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  bool is_invalid = false;
  memset(divs, 0, sizeof(divs));
  for (int i = 0; i < m; i++) {
    int a, b;
    int big, small;
    scanf("%d %d", &a, &b);
    big = max(a, b);
    small = min(a, b);
    if (divs[big] == 2 || divs[small] == 1) is_invalid = true;
    divs[big] = 1;
    divs[small] = 2;
  }
  if (is_invalid)
    printf("0");
  else {
    int num_independents = 0;
    int max_of_div2 = -1987654321;
    int min_of_div1 = 1987654321;
    divs[1] = 2;
    divs[n] = 1;
    for (int i = 1; i <= n; i++) {
      if (divs[i] == 1)
        min_of_div1 = min(min_of_div1, i);
      else if (divs[i] == 2)
        max_of_div2 = max(max_of_div2, i);
    }
    for (int i = 1; i <= n; i++) {
      if (divs[i] == 0 && i < min_of_div1 && i > max_of_div2)
        num_independents++;
    }
    if (max_of_div2 == -1987654321 || min_of_div1 == 1987654321 ||
        min_of_div1 < max_of_div2)
      printf("0");
    else
      printf("%d", num_independents + 1);
  }
  return 0;
}
