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<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//sort

int main(void){
	int N;
	cin>>N;
	int a[100001],b[100001];
	rep(i,N)cin>>a[i]>>b[i];
	sort(a,a+N);
	sort(b,b+N);
	int current=0,max=0;
	int acnt=0,bcnt=0;//N??§?????????
	int begin=-1;
	while(1){
		if(acnt<=N-1){
			if(a[acnt]<b[bcnt]){
				current++;
				begin=a[acnt];
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				acnt++;
			}else if(a[acnt]>b[bcnt]){
				current--;
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				bcnt++;
			}else{
				current++;
				acnt++;
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
				bcnt++;
				current--;
				if(current+1>=begin){
					if(current+1>b[bcnt]){
						if(b[bcnt]-1>max)max=b[bcnt]-1;
					}else{
						if(current>max)max=current;
					}
				}
			}
		}else{
			current--;
			if(current+1>=begin){
				if(current+1>b[bcnt]){
					if(b[bcnt]-1>max)max=b[bcnt]-1;
				}else{
					if(current>max)max=current;
				}
			}
			bcnt++;
		}
		if(acnt==N && bcnt==N)break;
	}
	cout<<max<<endl;
	return 0;
}

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

using namespace std;

int n;
int cnt[100005];

int main()
{
    cin >> n;
    int a, b;
    for(int i = 0; i < n; i++){
        cin >> a >> b;
        for(int j = a; j < b + 1; j++){
            cnt[j]++;
        }
    }

    int ans = 0;
    for(int i = 1; i < n + 2; i++){
        if(i <= cnt[i] + 1) ans = i - 1;
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    c[a]++;
    c[b]--;
  }
  for(int i = 0; i < N-1 ;i++){
    c[i+1] += c[i];
  }
  int max = 0;
  for(int i = 1; i < N ; i++){
    if(i == c[i]+1) max = i;
  }
  cout << max << endl;
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;


int main(){
	int n;
	int a[100000], b[100000];

	cin >> n;
	int max = 0, min = 100001;
	for (int i = 0; i < n; ++i){
		cin >> a[i] >> b[i];
		if (a[i] < min) min = a[i];
		else if (b[i] > max) max = b[i];
	}

	int max_friends = 1;
	for (int i = min; i < max; ++i){
		int friends = 0;
		for (int j = 0; j < n; ++j){
			if (a[j] <= i-1 && b[j] >= i-1){
				++friends;
			}
		}
		if (friends == i-1 && --friends > max_friends){
			max_friends = friends;
		}
	}

	cout << max_friends << endl;



	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int main(){
  int ans=0;
  int n,a,b;
  int imos[100005]={0};
  cin>>n;
  rep(i,n){
    scanf("%d%d",&a,&b);
    imos[a]++;
    imos[b+1]--;
    //cout<<imos[a]<<" "<<imos[b+1]<<endl;
  }
  REP(i,1,n+1){
    imos[i]+=imos[i-1];
    if(imos[i]>=ans)ans=imos[i]-1;
  }
  printf("%d\n",ans);
  return 0;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n,a,b,ans=-1;
	cin>>n;
	int dt[100002]={};
	rep(i,n){
		cin>>a>>b;
		dt[a]++;
		dt[b+1]--;
	}
	int cnt=-1;
	loop(i,1,100002){
		dt[i]+=dt[i-1];
	}
	rep(i,n+2){
		if(ans<dt[i])ans=dt[i];
	}
	if(n!=1)cout<<ans-1<<endl;
	else if(dt[2]!=0)cout<<1<<endl;
	else cout<<0<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;


signed main(){
    int n;;
    cin >> n;
    int d[100010] = {0};
    rep(i, 0, n){
        int a, b;
        cin >> a >> b;
        d[a - 1]++;
        d[b]--;
    }
    int MAX = 0;
    rep(i, 1, 100010){
        d[i] += d[i - 1];
        if(i == d[i] - 1) MAX = max(MAX , d[i] - 1);
    }
    cout << MAX << endl;
}

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

int dp[100002];

int main() {

    int n;

    cin >> n;

    int a,b;

    for(int i=0;i<n;i++){
        cin >> a >> b;
        a--;b--;
        for(int j=a;j<=b;j++){
            dp[j]++;
        }
    }

    int ans = 0;

    for(int i=2;i<=100001;i++){
        if(dp[i]>=i) {
            ans = max(ans, i);
        }
    }

    cout << ans << endl;

    return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
typedef pair<int, int> pii;

int N, A, B;
int s[200000];

int main(void) {
  cin >> N;
  REP(i, 0, N + 2) s[i] = 0;
  REP(i, 0, N) {
    cin >> A >> B;
    s[A]++;
    s[B]--;
  }
  REP(i, 1, N + 2) s[i] += s[i - 1];

  int ans = 0;
  REP(i, 0, N + 2) ans = max(ans, s[i]);
  cout << (ans == 1 ? 0 : ans) << endl;

  return 0;
}