#include <bits/stdc++.h>

using namespace std;

int main ()
{
	int N;
	cin >> N;
	vector<int>data ( 100005 );
	data[0]++;
	data[N + 4]--;
	for( size_t i = 0; i < N; i++ )
	{
		int a , b;
		cin >> a >> b;
		data[a]++;
		data[b + 1]--;
	}
	for( size_t i = 1; i < 100005; i++ )
	{
		data[i] += data[i - 1];
		//cout << data[i] << endl;
	}
	for( int i = N+1; i >= 0; i-- )
	{
		if( data[i] >= i )
		{
			cout << i -1<< endl;
			return 0;
		}
	}
}