#include <bits/stdc++.h>
using namespace std;

int main(){
	int n, a, b, i, ans = 0;
	cin >> n;
	vector<int> v(114514, 0);
	for (i = 0; i < n; i++){
		cin >> a >> b;
		a--;
		v[a]++;
		v[b]--;
	}
	for (i = 0; i <= n; i++){
		v[i + 1] = v[i] + v[i + 1];
		if (i <= v[i]) ans = max(i, ans);
	}
	cout << ans << endl;
}

