#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int m;
  cin >> n >> m;
  if (m == 0) {
    cout << n - 1 << endl;
  } else {
    int sim1[m];
    int div[n];
    int sim2[m];
    int largsmal = 0, smalarg = 1000000;
    for (int i = 0; i < m; i++) {
      cin >> sim1[i] >> sim2[i];
      if (sim1[i] > sim2[i]) {
        div[sim1[i] - 1] = 1;
        div[sim2[i] - 1] = 2;
        if (sim2[i] - 1 > largsmal) {
          largsmal = sim2[i] - 1;
        }
        if (sim1[i] - 1 < smalarg) {
          smalarg = sim1[i] - 1;
        }
      } else {
        div[sim1[i] - 1] = 2;
        div[sim2[i] - 1] = 1;
        if (sim1[i] - 1 > largsmal) {
          largsmal = sim1[i] - 1;
        }
        if (sim2[i] - 1 < smalarg) {
          smalarg = sim2[i] - 1;
        }
      }
    }
    if (smalarg <= largsmal) {
      cout << 0 << endl;
    } else {
      cout << smalarg - largsmal << endl;
    }
  }
  return 0;
}
