#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int ruisekiwa[100005];
int main()
{
	int a; cin >> a;
	for (int b = 0; b < a; b++) {
		int c, d; scanf("%d%d", &c, &d);
		ruisekiwa[c]++; ruisekiwa[d + 1]--;
	}
	int k = 1;
	int MAX = 0;
	for (int i = 0; i < 100005; i++) {
		k += ruisekiwa[i];
		if (k >= i)MAX = max(MAX, min(k, i));
	}
	cout << MAX-1 << endl;
}