Go: // A Way to Invite Friends(友だちの誘い方)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=ja
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const N_MAX = 1000001

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)
	allow_list := make([]int, N_MAX+100)
	nMax := 0

	for i := 0; i < n; i++ {
		a, b := nextInt(sc), nextInt(sc)
		allow_list[a] += 1
		allow_list[b+1] += -1
		nMax = int(math.Max(float64(nMax), float64(b)))
	}

	for i := 2; i <= N_MAX; i++ {
		allow_list[i] += allow_list[i-1]
	}

	res := 0
	for i := 2; i <= nMax; i++ {
		if i-1 <= allow_list[i] { // i人誘う時に行きたい人の人数を超えないか(自分を含めて)
			res = i - 1 // 自分は除く
		}
	}

	fmt.Println(res)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  std::cin >> n;
  vector<int> f(1e6 + 1, 0);
  int a, b, ans = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a >> b;
    f[a]++;
    f[b + 1]--;
  }
  for (int i = 1; i <= 1e6; i++)f[i] += f[i - 1];
  for (int i = 0; i <= 1e6; i++) {
    ans = max(ans, (i <= f[i])?f[i]:0);
  }
  std::cout << max(0, ans - 1) << std::endl;
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)



int main(void){
	//come[i] := i???(?????????????????????)??§??????????????¨?????????????????????????????????????????°
	int come[100010];

	rep(i, 100010) come[i] = 0;//0??§?????????

	int n; scanf("%d", &n);
	rep(i, n){
		int a, b;
		scanf("%d %d", &a, &b);
		reps(j, a, b + 1){
			come[j]++;
		}
	}

	
	int ans = 0;
	reps(i, 2, 100010){
		//???????????????????????????????????????i?????§????????°??\????????????????????????
		//come[i]????????????????????§???come[i] + 1??????i???????¶????????????????????¢??????????
		if(come[i] + 1 >= i){
			//??????????????????i?????§????????°??\??????????????¨???????????¶?????????????????§???
			//???????????\???????????????????????°???i-1???
			ans = max(ans, i - 1);
		}
	}
	printf("%d\n", ans);

	return 0;
}

C++: #include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,a,b,d[N],ans;

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    d[a]++;
    d[b+1]--;
  }
  for(int i=1;i<=n+1;i++)d[i]+=d[i-1];
  ans=1;
  for(int i=n+1;i>=2;i--)
    if(d[i]>=i-1)ans=max(ans,i);
  cout<<ans-1<<endl;
  return 0;
}

C++: #include<bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int n;cin>>n;
  pair<int,int>fri[n];
  for(int i=0;i<n;i++)cin>>fri[i].F>>fri[i].S;
  int le=0,ri=n+1;
  while(ri-le>1){
    int mid=(le+ri)/2;
    int cnt=1;
    for(int j=0;j<n;j++){
      if(fri[j].F<=mid&&mid<=fri[j].S)cnt++;
    }
    if(cnt>=mid)le=mid;
    else ri=mid;
  }
  cout<<le-1<<endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(){
    int N;
    cin >> N;
    vector<int> imos(100010,0);
    for(int i=0;i<N;i++){
        int a,b;
        cin >> a >> b;
        imos[a]++; imos[b+1]--;
    }
    int ans = 0;
    for(int i=1;i<=100002;i++){
        imos[i] += imos[i-1];
        if(i-1<=imos[i]) ans = i-1;
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <stdlib.h>

int main(void){
	int n;
	std::cin >> n;
	int endmax = 0;

	int* pn = new int[100000];
	memset(pn, 0, 100000 * sizeof(int));

	for(int i=0; i<n; i++){
		int beg, end;
		std::cin >> beg >> end;
		for(int i = beg; i <= end; i++) pn[i]++;
		if(end > endmax) endmax = end;
	}

	int mps = 1;
	for(int i=1; i<=endmax; i++){\
		if(pn[i]+1 >= i)mps = i;
	}

	std::cout << (mps-1) << '\n';
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int a[MAX_N],b[MAX_N];
int n;

bool possible(int u)
{
    int cnt = 1;
    rep(i,n){
        if(a[i] <= u && b[i] >= u){
            cnt++;
            if(cnt >= u){
                return true;
            }
        }
    }
    return false;
}

int main()
{
	scanf("%d",&n);
	rep(i,n){
		scanf("%d%d",&a[i],&b[i]);
	}
    int h,l;
    l = 1,h = n+2;
    while(1){
        int mid = (l + h) / 2;
        if(mid == l){
            printf("%d\n",mid-1);
            break;
        }
        if(possible(mid)){
            l = mid;
        }else{
            h = mid;
        }
    }
	return 0;
}

C++: #include <iostream>
using namespace std;

int num[100002];

int main() {
	int n;
	
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		num[a - 1]++;
		num[b]--;
	}
	for (int i = 1; i < 100002; i++) {
		num[i] += num[i - 1];
	}
	
	for (int i = 100001; i >= 0; i--) {
		if (num[i] >= i) {
			cout << i << endl;
			return 0;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>

#define REP(i,n) for (int i=0; i<n; i++)


using namespace std;

int main(){
	int n, a, b, fr[100005], mx=0, ans;
	cin >> n;
	REP(i,100005){ fr[i] = 0;}
	fr[0] = 1;
	REP(i,n){
		cin >> a >> b;
		fr[a]++;
		fr[b+1]--;
	}
	REP(i,100004){
		fr[i+1] += fr[i];
	}
	for (int i=0; i<=n; i++){
		if (fr[i]>ans && fr[i]>=i){
			ans = min(i, fr[i]);
		}
	}
	
	
	//REP(i,10) cerr << fr[i] << " ";
	//cerr << endl;
	
	cout << ans-1 << endl;
	
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());

#define MAX_SIZE 1000100
// int a[100010]={0};
ll all[MAX_SIZE - 1], part[MAX_SIZE - 1]; // segment tree
void add(int a,int b,int x,int k,int l,int r){
	if(a<=l&&r<=b){
		all[k]+=x;
	}
	else if(l<b&&a<r){
		part[k]+=(min(b,r)-max(a,l))*x;
		add(a,b,x,k*2+1,l,(l+r)/2);
		add(a,b,x,k*2+2,(l+r)/2,r);
	}
}

ll sum(int a,int b,int k,int l,int r){
	// if(k>100010) cout<<"hoge"<<endl;
	// cout<<k<<" "<<l<<" "<<r<<endl;
	if(r<=a||b<=l){
		return 0;
	}
	if(a<=l&&r<=b){
		return all[k]*(r-l)+part[k];
	}

	else{

		ll res;
		res=(min(b,r)-max(a,l))*all[k];
		res+=sum(a,b,k*2+1,l,(l+r)/2);
		res+=sum(a,b,k*2+2,(l+r)/2,r);
		return res;
	}
	return 0;
	// return getsum(a,b,k,l,(l+r)/2)+getsum(a,b,k,(l+r)/2,r));
}



int main(){
	int n;
	cin>>n;
	rep(i,n){
		int a,b;
		cin>>a>>b;
		// cout<<a<<" "<<b<<endl;
		add(a,b+1,1,0,0,100005);
		// cout<<sum(a,b,0,0,100005)<<endl;
	}
	// ll ans=0;
	// cout<<0<<endl;
	int ans=0;
	rep(i,100005){
		ll t=sum(i+1,i+2,0,0,100005);
		if(i+1<=(int)t){
			ans=max(ans,i);
		}
	}
	cout<<ans<<endl;
}