#include <iostream>
#include <algorithm>
#define N 100000
int a[N],b[N],c[N];
using namespace std;
int main( void )
{
  int i,j,k,n,d=0,ret=0;
  cin >> n;
  for( i=0; i<n; i++ )
    cin >> a[i] >> b[i];
  sort( a, a+n );
  sort( b, b+n );
  for( i=2,j=0,k=0; i<=n+1; i++ ){
    while( a[j] == i ){ j++; d++; }
    while( b[k] == i-1 ){ k++; d--; }
    if( i -1 <= d ) ret = i-1;
  }
  cout << ret << endl;
  return 0;
}