#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j;
  while (scanf("%d%d", &i, &j) != EOF) {
    int a = i, b = 1;
    int a1, a2;
    for (int k = 0; k < j; k++) {
      cin >> a1;
      cin >> a2;
      if (a1 < a2) {
        int t = a1;
        a1 = a2;
        a2 = t;
      }
      a = min(a1, a);
      b = max(a2, b);
    }
    if (a - b > 0)
      cout << a - b << endl;
    else {
      cout << "0" << endl;
    }
  }
  return 0;
}
