#include <bits/stdc++.h>
using namespace std;
struct node {
  int id;
  int mark;
} pro[100100];
struct node1 {
  int l, r;
} sim[100100];
bool cmp(const node1 s1, const node1 s2) { return s1.l < s2.l; }
bool cmp2(const node1 s1, const node1 s2) { return s1.r < s2.r; }
int n, m;
int main() {
  while (scanf("%d%d", &n, &m) == 2) {
    int ju = 0;
    if (m == 0) {
      printf("%d\n", n - 1);
      continue;
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &sim[i].l, &sim[i].r);
      if (sim[i].l > sim[i].r) {
        int w1 = sim[i].l;
        int w2 = sim[i].r;
        sim[i].l = w2;
        sim[i].r = w1;
      }
    }
    sort(sim + 1, sim + 1 + m, cmp);
    int tem1 = sim[m].l;
    sort(sim + 1, sim + 1 + m, cmp2);
    int tem2 = sim[1].r;
    for (int i = 1; i <= m; i++) {
      if (tem1 >= tem2) {
        printf("0\n");
        ju++;
        break;
      }
    }
    if (!ju) printf("%d\n", tem2 - tem1);
  }
  return 0;
}
