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;
using ll=long long;
 
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m);i>=(int)(n);--i)
#define debug(x) cout << #x << "=" << (x) << endl;
 
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end

void solve(){
  int N;
  cin>>N;
  vector<int> imos(101010);
  rep(i,N){
    int a,b;
    cin>>a>>b;
    a--;b--;
    imos[a]++;
    imos[b+1]--;
  }
  rep(i,imos.size()-1)
    imos[i+1]+=imos[i];
  int m=0;
  rep(i,imos.size())
    if(imos[i]>=i)
    m=max(m,i);
  cout<<m<<endl;
}
 
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  solve();
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define fst	first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;

int main() {
	int n;	cin >> n;
	vint sum(100010, 0);
	rep(i, n) {
		int a, b;	cin >> a >> b;
		sum[a]++;	sum[b + 1]--;
	}
	srep(i, 1, 100010)	sum[i] += sum[i - 1];
	int ret = 1;
	REP(i, n + 1) {
		if (sum[i] + 1 >= i)	ret = max(ret, i);
	}
	cout << ret - 1 << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
#define enld '\n'
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int> l(100005,0),r(100005,0);
    for(int i=0; i<N; i++){
        int a,b;
        cin >> a >> b;
        l[a]++;
        r[b]++;
    }
    int ans = 0;
    int now = l[0]+1;
    for(int i=1; i<100005; i++){
        now -= r[i-1];
        now += l[i];
        if(i <= now){
            ans = i-1;
        }
    }
    cout << ans << enld;
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=1; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=1; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#define all(c) c.begin(),c.end()
#include<vector>
#define MAX 100010
using namespace std;
int main(){
	int n;
	cin>>n;
	int ans=0;
	int a[MAX],b[MAX];
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	for(int i=1;i<=n+1;i++){
		vector<int> vec;
		for(int j=0;j<n;j++){
			if(i>=a[j]){
				vec.push_back(b[j]);
			}
		}
		//cout<<i<<endl;
		sort(all(vec));
		int t=0;
		for(int k=0;vec.size()>k;k++){
			if(i<=vec[k]){
			t++;
			//cout<<"------"<<endl;
			}
		}
		//int t=(lower_bound(vec.begin(),vec.end(),i)-vec.begin());
		ans=max(ans,t);
		/*for(int k=0;k<(int)vec.size();k++)
			cout<<vec[k];
		cout<<endl;
		cout<<"  "<<t<<endl;
		*/
		vec.clear();
	}
	
	cout<<ans-1<<endl;
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
	int N;
	cin >> N;
	vector<int> a(100010);
	for (int i = 0; i < N; ++i) {
		int x, y;
		cin >> x >> y;
		++a[x-1];
		--a[y];
	}
	int c = 0;
	int r = 0;
	for (int i = 1; i < a.size(); ++i) {
		c += a[i];
		if (c >= i) {
			r = i;
		}
	}
	cout << r << endl;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    EACH(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
    REP(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
    REP(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll n; cin >> n;
    map<ll, ll> m;
    REP(i, n) {
        ll a, b; cin >> a >> b;
        m[a]++;
        m[b] = m[b];
        m[b+1]--;
    }
    ll ans = 1;
    ll cnt = 1;
    EACH(p, m) {
        ll need, d; tie(need, d) = p;
        cnt += d;
        if (cnt >= need) {
            // cout << need << " " << d << " " << cnt << endl;
            ans = max(ans, need);
        }
    }
    cout << ans-1 << endl;
}