#include<cstdio>
#include<algorithm>
using namespace std;
static const int MAX_N = 100000;

int N;
int memo[MAX_N + 3];

int main(){
	scanf("%d", &N);
	for(int i = 0; i < N; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		memo[a]++; memo[b + 1]--;
	}
	int res = 0;
	for(int i = 1; i <= N + 1; i++){
		memo[i] += memo[i - 1];
		if(memo[i] + 1 < i) continue;
		res = max(res, min(memo[i], i - 1));
	}
	printf("%d\n", res);
	return 0;
}
