#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int arr[100007];
  int l1 = 0, l2 = n + 1;
  if (m == 0) {
    cout << n - 1 << endl;
    return 0;
  }
  int flag = 1;
  memset(arr, 0, sizeof(arr));
  for (int i = 0; i < m; i++) {
    int k, t;
    cin >> k >> t;
    int t1 = min(k, t);
    int t2 = max(k, t);
    l1 = max(l1, t1);
    l2 = min(l2, t2);
    if (arr[t1] == 2 || arr[t2] == 1) {
      flag = 0;
    }
    arr[t1] = 1;
    arr[t2] = 2;
  }
  if (flag == 0 || l1 > l2) {
    cout << "0" << endl;
    return 0;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == 0) {
      if (i > l1 && i < l2) cnt++;
    }
  }
  cout << cnt + 1 << endl;
}
