#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int col[MAXN];
int n, m;
bool paint(int x, int y) {
  if (x > y) swap(x, y);
  if (col[x] >= 0 && col[y] >= 0) {
    if (col[x] == col[y])
      return false;
    else {
      col[x] = 0;
      col[y] = 1;
    }
  } else {
    if (col[x] == 1 || col[y] == 0) return false;
    col[x] = 0;
    col[y] = 1;
  }
  return true;
}
int main() {
  memset(col, -1, sizeof(col));
  scanf("%d%d", &n, &m);
  bool flag = true;
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (!paint(x, y)) {
      flag = false;
    }
  }
  bool flag1 = 0;
  int ans = 0, cnt[2] = {0, 0};
  for (int i = 1; i <= n; i++) {
    cnt[col[i]]++;
    if (col[i] == 1) flag1 = 1;
    if (!flag1 && col[i] < 0) ans++;
    if (col[i] == 0) ans = 0;
    if (flag1 && col[i] == 0) flag = false;
    if (flag1) col[i] = 1;
  }
  if (!cnt[0]) ans--;
  if (!cnt[1]) ans--;
  if (flag) {
    printf("%d\n", ans + 1);
  } else {
    puts("0");
  }
  return 0;
}
