#include<bits/stdc++.h>

using namespace std;

int main(){

  int n;

  cin >> n;

  vector<int> a(n);
  vector<int> b(n);
  vector<int> num(1000000,0);
  int min = 1000000;
  
  for(int i = 0;i < n;++i){
	cin >> a[i] >> b[i];
	if(min > a[i])
	  min = a[i];
	for(int j = a[i];j <= b[i];++j)
	  num[j]++;
  }

  int max_fri = 0;
  
  for(int i = min;i <= n + 1;++i){
	for(int j = 0;j < n;++j){
	  if(i <= num[i] + 1 & max_fri < i - 1)
		max_fri = i - 1;
	}
  }
  
  cout << max_fri << endl;
  
}

