#include<stdio.h>
#include<math.h>
#include<algorithm>

int change[100010] = {};
int main() {
	int N = 0;
	scanf("%d",&N);
	for (int i = 0; i < N;i++) {
		int a, b;
		scanf("%d %d",&a,&b);
		change[a]++;
		change[b+1]--;
	}
	int ans=0;
	int p=0;
	for (int i = 2; i <= N + 1; i++) {
		p += change[i];
		if (i-1 <= p) {
			ans = std::max(ans, i-1);
		}
	}
	printf("%d\n", ans);
	return 0;
}