#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> P;
const int mod = 1000000007;

int sum[100010];

signed main(){

    int n;
    cin >> n;
    rep(i, n){
        int a, b;
        cin >> a >> b;
        sum[a]++;
        sum[b+1]--;
    }
    rep(i, 100009)sum[i+1] += sum[i];
    int ans = 0;
    rep(i, n+2)if(sum[i] >= i - 1)ans = max(ans, i - 1);
    cout << ans << endl;
}
