#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
bool vis[maxn];
vector<int> Div1, Div2;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int u, v;
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &u, &v);
    if (u < v) {
      swap(u, v);
    }
    Div1.push_back(u);
    Div2.push_back(v);
  }
  if (m == 0) {
    printf("%d\n", n - 1);
  } else {
    int a = *max_element(Div2.begin(), Div2.end());
    int b = *min_element(Div1.begin(), Div1.end());
    printf("%d\n", max(0, b - a));
  }
  return 0;
}
