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;
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,b) FOR(i,0,b)
#define RFOR(i,a,b) for (int i = a-1; i >= b; i--)
#define RREP(i,a) RFOR(i,a,0)
#define REPALL(i,v) for (int i = 0; i < v.size(); i++)
#define RREPALL(i,v) for (int i = v.size()-1; i >= 0; i--)
#define SORT(v) sort(v.begin(), v.end())
#define MIN_ELEMENT(v) min_element(v.begin(), v.end())
#define MAX_ELEMENT(v) max_element(v.begin(), v.end())
#define COUNT(v,n) count(v.begin(), v.end(), n);
void YES(bool flag) {cout<<(flag ? "YES" : "NO")<<endl;}
void Yes(bool flag) {cout<<(flag ? "Yes" : "No")<<endl;}
void yes(bool flag) {cout<<(flag ? "yes" : "no")<<endl;}
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

vector<int> a,b, sum(111111);
int ans = 1;

int main()
{
    int n;
    cin>>n;
    a.resize(n); b.resize(n);
    REP(i,n) {
        cin>>a[i]>>b[i];
        sum[a[i]]++;
        sum[b[i]+1]--;
    }
    sum[0]++;
    REP(i,100111) {
        sum[i+1] = sum[i] + sum[i+1];
        if (sum[i+1] >= i+1) {
            ans = i+1;
        }
    }
    cout<<ans - 1<<endl;

    return 0;
}

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

#define REP(i,a) for(long i=0;i<(a);++i)

typedef std::vector<int>  VI;

int N;
VI imos;
int main() {
    std::cin >> N;
    imos.resize(N+3, 0);

    REP(i, N) {
        int a, b;
        std::cin >> a >> b;
        ++imos[a];
        --imos[b+1];
    }

    int x = 0, ans = 0;
    for(int i=0; i<=N+1; ++i) {
        x += imos[i];
        if(x + 1 >= i) {
            ans = i - 1;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

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

using namespace std;
	

int main(){
	int N;
	vector<pair<int,int>> friends;
	vector<int> humans;
	vector<int> nokemono;

	//INPUT
	cin >> N;
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		friends.push_back(make_pair(a,b));
	}
	//UPCOUNT & DOWNCOUNT
	for(int i=0;i<=N+1;i++){
		int count = 0;
		int minn = 999999;
		for(auto range:friends){
			if(i >= range.first && i <= range.second){
				count++;
				if(range.second < minn)
					minn = range.second;
			}
		}
		count++;
		humans.push_back(count);
		if(minn == 999999) minn = 0;
		nokemono.push_back(minn);
		count = 0;
	}

	//bool flag = true;
	int maxn = 0;
	for(int i=N+1;i>0;i--){
		if(humans[i] < nokemono[i]) nokemono[i] = humans[i];
		if(humans[i] < i){
			//cout << i << endl;
			nokemono[i] = 0;
		}
		if(nokemono[i] > maxn) maxn = nokemono[i];
		//if(nokemono[i] < humans[i]) nokemono[i] = 0;
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
		//cout << i << "," << humans[i]-1 << endl;
		//flag = false;
		//break;
	}
	

	cout << max(0,maxn-1) << endl;
	return 0;
}

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

#define fs first
#define sc second
typedef pair<int,int> P;

int n;
P p[200100];

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> p[2*i].fs >> p[2*i+1].fs;
    p[2*i].sc = 0; p[2*i+1].sc = 1;
  }
  p[2*n] = P(1,0); p[2*n+1] = P(100001,1);

  sort(p,p+2*(n+1));

  int num = 0, cnt = 0, ans = 0;
  for(int i=0;i<=n+1;i++){
    if(p[num].fs == i){
      if(!p[num].sc)cnt++;
      else cnt--;
      if(cnt == i)ans = i;
      num++; i--;
    }else if(cnt == i)ans = i;
  }

  cout << ans-1 << endl;
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int i;
	int N=read();
	int a[100002]={0},b[100002]={0};
	int sum[N+1]={0};
	
	sum[0] = 1; //include me
	
	REP(i,N){
		a[read()] ++;
		b[read()] --;
	}
	
	for(i=1;i<=N;i++){
		sum[i] += sum[i-1] + a[i] + b[i];
		//cout << sum[i] << endl;
	}
	
	for(i=N;i>=0;i--){
		if(sum[i]>=i){
			cout << i-1;
			return 0;
		}
	}
	cout << 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 100002;

int main(){
  int n,ans[MAX],a,b;
  int MX = -1<<27;
  int MN = 1<<27;
  fill(ans,ans+MAX,0);

  cin >> n;
  while(n--){
    cin >> a >> b;
    MX = max(MX,b);
    MN = min(MN,a);
    for(int i = a ; i <= b ; i++)
      ++ans[i];
  }
  int fri = 0;
  for(int i = MN ; i <= MX ; i++){
    if(ans[i]+1 > i) fri = i-1; 
  }
  cout << fri << 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;
	rep(i, 100010){
		//???????????????????????????????????????i?????§????????°??\????????????????????????
		//come[i]????????????????????§???come[i] + 1??????i???????¶????????????????????¢??????????
		if(come[i] + 1 >= i){
			//??????????????????i?????§????????°??\??????????????¨???????????¶?????????????????§???
			//???????????\???????????????????????°???i-1???
			ans = max(asn, i - 1);
		}
	}
	printf("%d\n", --ans);

	return 0;
}

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;
        for(int j=a;j<=b;j++){
            dp[j]++;
        }
    }

    int ans = 0;

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

    cout << ans << endl;

    return 0;
}

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

//#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,x,...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__,REP1,REP2,REP3))(__VA_ARGS__)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond) if(cond) break; else

template<typename T>
inline void input(vector<T>& v) { for (auto& x : v) cin >> x; }

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

const int INF = 1 << 29;

int s[100004];
signed main() {
	int n;
	while (cin >> n && n) {
		memset(s, 0, sizeof(s));
		rep(i, n) {
			int a, b; cin >> a >> b;
			s[a]++;
			s[b + 1]--;
		}
		rep(i, n + 2) s[i + 1] += s[i];
		int ans = 0;
		rep(i, n + 2) if(s[i] >= i - 1) {
			ans = i - 1;
		}
		cout << ans << endl;
	}
}