#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
long long int n, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;
int main() {
  long long int a, b;
  cin >> n;
  vector<long long int> va, vb;
  for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
    cin >> a >> b;
    va.push_back(a);
    vb.push_back(b);
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  if (va[va.size() - 1] - 1 > n) {
    cout << 0 << endl;
    return 0;
  }
  if (vb[0] - 1 > n) {
    cout << 0 << endl;
    return 0;
  }
  cout << max(va[va.size() - 1] - 1, vb[0] - 1) << endl;
}
