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<vector>
#include<algorithm>
#include<string.h>
using namespace std;

class range{
public:
	int left,right;
	range(int left,int right):left(left),right(right){}
	range(){}
};

bool operator < (range A,range B){
	if(A.left!=B.left) return A.left<B.left;
	return A.right<B.right;
}

int main()
{
	int N;
	cin>>N;

	int left[100002],right[100002];
	memset(left,0,sizeof(left));
	memset(right,0,sizeof(right));

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

	int before=left[2];
	int ans=1;
	if(left[2]>=2) ans=2;

	for(int num=3;num<=N;num++){
		int now=before+left[num]-right[num-1];
		if(now>=num) ans=max(ans,num);
		before=now;
	}
	cout<<ans-1<<endl;
}

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

int main(){
  int N, a, b, l, r, t;
  cin >> N;
  vector<int> A, B;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  l = 1;
  r = N+2;
  while (r - l > 1) {
    t = 1;
    for (int i = 0; i < N; ++i) {
      if ((r+l)/2 >= A[i] && (r+l)/2 <= B[i]) t++;
    }
    if (t >= (r+l)/2) l = (r+l)/2;
    else r = (r+l)/2;
  }
  cout << l-1 << endl;
}

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];
		cout << max(*max_element(s, s + n + 1) - 1, s[n + 1]) << endl;
	}
}

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], 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 <algorithm>
#include <vector>

using namespace std;

struct Tag {
    int min, max;

    bool operator < (const Tag &other) const {
        return max > other.max; // Reverse sort
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int N;
    cin >> N;
    vector<Tag> v(N);
    for(int i = 0; i < N; ++i) {
        cin >> v[i].min >> v[i].max;
        v[i].min--;
        v[i].max--;
    }
    sort(v.begin(), v.end());
    int left = 0, right = N+1;
    while(left+1 < right) {
        int n = (left+right)/2;
        int cnt = 0;
        for(vector<Tag>::iterator it = v.begin(); it != v.end(); ++it) {
            if(it->max < n) break;
            if(it->min <= n) ++cnt;
        }
        if(cnt >= n) left = n;
        else right = n;
    }
    cout << left << endl;
    return 0;
}

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

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;


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

	int N;
	cin >> N;

	vector<int> dp(100100, 0);
	FOR(i, 0, N) {
	    int a, b;
	    cin >> a >> b;
	    dp[a] += 1;
	    dp[b + 1] -= 1;
	}

	FOR(i, 0, dp.size() - 1) {
	    dp[i + 1] += dp[i];
	}

	int ans = 0;
	FOR(i, 0, dp.size()) {
	    if (dp[i] >= i - 1) {
            ans = max(ans, i - 1);
        }
	}
	print(ans);

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const long long INF = 1e7+1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector < pair<ll, ll > > vp;
typedef vector <string> vs;
typedef vector <char> vc;
typedef list <ll> lst;

ll n, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/

int main()
{
	ll a, b;
	cin >> n;
	vi v(1000010);
	v[0] = 0;
	REP(i, n)
	{
		cin >> a >> b;
		--a;
		v[a]++;
		v[b]--;
	}
	REP(i, 100001)
	{
		v[i + 1] += v[i];
	}
	REP(i, n + 1)
	{
		if (v[i] >= i)
			k = max(k, i);
	}
	cout << k << endl;
	return 0;
}