#include <bits/stdc++.h>
using namespace std;
class Problem_for_Round {
 public:
  void algorithm() {
    input();
    split();
    output();
  }

 private:
  int num_problem, num_pair;
  vector<int> *problem;
  int result, max;
  void input() {
    cin >> num_problem >> num_pair;
    problem = new vector<int>[num_problem + 1];
    int x, y;
    for (int i = 1; i <= num_pair; i++) {
      cin >> x >> y;
      problem[x].push_back(y);
      problem[y].push_back(x);
    }
    result = -1;
  }
  void split() {
    max = 1;
    for (int i = 2; i < num_problem; i++) {
      if (not_small(i) == 1)
        max = i;
      else
        break;
    }
    for (int i = max + 1; i <= num_problem; i++) {
      if (not_large(i) == 0) {
        result = 0;
        break;
      }
    }
  }
  int not_small(int x) {
    for (int i = 0; i < problem[x].size(); i++)
      if (problem[x][i] < x) return 0;
    return 1;
  }
  int not_large(int x) {
    for (int i = 0; i < problem[x].size(); i++)
      if (problem[x][i] > x) return 0;
    return 1;
  }
  void output() {
    if (result != 0) {
      result = 1;
      for (int i = max; i > 1; i--) {
        if (not_large(i) == 1)
          result++;
        else
          break;
      }
    }
    cout << result << endl;
  }
};
int main() {
  Problem_for_Round test;
  test.algorithm();
  return 0;
}
