//#include "bits/stdc++.h"

#include <iostream>
#include <array>
// #include <map>
// #include <string>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

array<int, 100002> box; // use key 100001

int n;
int np;
int min_;
int max_;

int solve() {
	cin >> n;
	np = n + 1;
	for (int i = 0; i < n; i++) {
		cin >> min_ >> max_;
		if (min_ > np) {
			continue;
		}
		box[min_ - 1]--;
		max_ = min(max_, np);
		box[max_]++;
	}
	box[np + 2] = 0;
	for (int i = np + 1; i >= 0; i--) {
		box[i] += box[i+1];
		//std::cout << "debug " << i << " " << box[i] << std::endl; // debug
		if (i - 1 <= box[i]) {
			return i - 1;
		}
	}
	return 0;
}

int main() {

	std::ios::sync_with_stdio(false);
	std::cin.tie(false);


	cout << solve() << endl;

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}