#include <bits/stdc++.h>
template <class T>
T ceil(T a, T b) {
  return (a + b - 1) / b;
}
template <class T>
void print(T x) {
  std::cout << x << std::endl;
}
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, -1, 0, 1}, dx = {1, 0, -1, 0};
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> dp(100003, 0);
  for (int i = (0); i < ((int)N); ++i) {
    int a, b;
    cin >> a >> b;
    dp[a] += 1;
    dp[b + 1] -= 1;
  }
  for (int i = (0); i < ((int)dp.size() - 1); ++i) {
    dp[i + 1] += dp[i];
  }
  int ans = 0;
  for (int i = (0); i < ((int)N + 1); ++i) {
    ans = max(ans, min(dp[i], i));
  }
  print(ans - 1);
  return 0;
}
