#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a > b ? b : a; }
int main() {
  int n, m;
  cin >> n >> m;
  int left = 1;
  int right = n;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) {
      int temp = x;
      x = y;
      y = temp;
    }
    left = max(left, x);
    right = min(right, y);
  }
  cout << max(right - left, 0);
  return 0;
}
