#include <bits/stdc++.h>
using namespace std;
long long n, a[100010], b[100010], ma = 0, mi = 100010;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] > ma) ma = a[i];
    if (b[i] < mi) mi = b[i];
  }
  if (mi - ma < 0 || ma > n + 1)
    cout << 0 << endl;
  else if (n + 1 < mi) {
    cout << n << endl;
  } else {
    cout << mi - 1 << endl;
  }
  return 0;
}
