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 <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

void solve(int N) {
    VI seg(100010, 0);
    REP(i, N) {
        int a, b;
        scanf("%d%d", &a, &b);
        seg[a]++;
        seg[b + 1]--;
    }
    REP(i, 100008) seg[i + 1] += seg[i];
    int ans = 0;
    FOR(i, 1, N + 1) {
        //cout << seg[i] << " ";
        if (seg[i] >= i - 1) ans = i - 1;
    } 
    cout << ans << endl;
    return;
}

signed main(void) {
    LL n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    while (cin >> n, n) {
        solve(n);
        return 0;
    }
}

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

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

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=1<<29,MOD=1e9+7;

int main(){
	int n; cin>>n;
	int a[100010]={};
	for(int i=0;i<n;i++){
		int x,y; cin>>x>>y;
		a[x]++; a[y+1]--;
	}
	int ans=0;
	for(int i=0;i<100009;i++){
		a[i+1]+=a[i];
		if(i<=a[i]&&ans<i) ans=i;
	}
	cout<<--ans<<endl;
	return 0;
}

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



int main() {
	int N;
	int num[100002];

	int sum;


	cin >> N;

	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		for (int j = a; j <= b; j++) {
			num[j]++;
		}
	}

	
	for (int i = N + 1; i > 0; i--) {
		if (num[i] >= i - 1) {
			cout << i - 1 << endl;
			break;
		}
	}



	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main(void){
    int n;
    cin >> n;
    int a[101010] = {};
    int l, r;
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        a[l]++;
        a[r+1]--;
    }
    for (int i = 0; i < n+2; i++) a[i+1] += a[i];
     
    int ans = 0;
    for (int i = 2; i < n+2; i++) {
        if (a[i]+1 >= i) {
            ans = max(ans, i);
        }
    }
    if (ans) ans--;
    cout << ans << endl;
}

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]++;
        d[b + 1]--;
    }
    int ans = 0;
    rep(i, 1, 100010){
        d[i] += d[i - 1];
        if(i == d[i]) ans = i - 1; 
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

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

int solve() {
	int ok = 0, ng = 100001;
	while(abs(ok - ng) > 1) {
		int m = (ok + ng) / 2;
		int cnt = 1;
		for(int i = 0; i < n; i++) {
			if(a[i] <= m && m <= b[i]) cnt++;
		}
		if(cnt >= m) ok = m;
		else ng = m;
	}
	return ok - 1;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(cin >> n) {
		for(int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
		}
		cout << solve() << endl;
	}
}

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



int main() {
	int N;
	int num[100002];

	cin >> N;
	for (int i = 0; i < N; i++){
		num[i] = 0;
	}

	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		for (int j = a; j <= b && j <= N; j++) {
			++num[j];
		}
	}

	
	for (int i = N + 1; i > 0; i--) {
		if (num[i] >= i - 1) {
			cout << i - 1 << endl;
			break;
		}
	}



	return 0;
}

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

#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(int i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define sz(x) ((int)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair
using ll  = long long;
using vi  = vector<int>;
using vvi = vector<vi>;
using vs  = vector<string>;
using pii = pair<int,int>;
constexpr ll inf = 1ll<<61;
constexpr ll mod = 1e9+7;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>void add(T &a, const T &b){a += b; a %= mod;}

signed main(){
  int n;
  cin >> n;
  vi a(n), b(n);
  rep(i,n) cin >> a[i] >> b[i];
  vi imos(100010);
  rep(i,n){
    imos[a[i]]++;
    imos[b[i]+1]--;
  }
  rep(i,imos.size()-1){
    imos[i+1] += imos[i];
  }
  dump(imos);
  ROF(i,n+2,0){ // i人で行く=> i-1人誘う
    if(imos[i] >= i-1){
      cout << i-1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
}

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

int N;


int main() {
    cin >> N;
    int memo[101010];
    fill(memo, memo+101010, 0);
    for (int j = 0; j < N; ++j) {
        int a, b;
        cin >> a >> b;
        ++memo[a];
        --memo[b+1];
    }
    int ret = 1;
    for (int j = 2; j < 101010; ++j) {
        memo[j] += memo[j-1];
        if (memo[j] >= j-1) {
            ret = max(ret, memo[j]);
        }
    }
    cout << ret-1 << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
int n,a[100000],b[100000];
int k[100002];
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i]>>b[i];
		k[a[i]]++;k[b[i]+1]--;
	}
	for(int i=1;i<=100001;i++)k[i]+=k[i-1];
	int maxn=0;
	for(int i=1;i<=100001;i++){
		if(i-1<=k[i]){
			maxn=i-1;
		}
	}
	cout<<maxn<<endl;
	return 0;
}