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++: #define _CRT_SECURE_NO_WARNINGS
#include"bits/stdc++.h"
#define INF          1e9
#define INFLL        1e18
#define EPS          1e-9
#define MOD          1000000007
#define REP(i,n)     for(ll i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n)    for(ll i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n)    for(ll i=(n)-1;i>=0;--i)
#define REPR1(i,n)   for(ll i=(n);i>0;--i)
#define REPC(i,obj)  for(auto i:obj)
#define ALL(obj)     (obj).begin(),(obj).end()
#define SETP(n)      cout<<fixed<<setprecision(n)
#define DELNL        cin.ignore(numeric_limits<streamsize>::max(),'\n')
#define VV(T,h,w)    vector<vector<T>>(h,vector<T>(w))
#define VVI(T,h,w,i) vector<vector<T>>(h,vector<T>(w,i))
using namespace std;
using ll = long long;
template<typename T = ll>inline T in() { T ret; cin >> ret; return ret; }

int main()
{
    vector<ll>imos(100003, 0);
    REP(i, in()) {
        ll a = in(), b = in();
        ++imos[a];
        --imos[b + 1];
    }
    REP(i, imos.size() - 1)imos[i + 1] += imos[i];
    ll ans = 0;
    REP(i, 100002)if (imos[i] >= i - 1)ans = max(ans, i - 1);
    cout << ans << endl;
}

C++: // TLE???
#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
#define MAX 100100
int x[MAX];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  for (int i = 0; i < MAX; i++) x[i]=0;
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int l,r; cin>>l>>r;
    if (l > N+1) continue;
    r=min(r,N+1);
    for (int j = l; j <= r; j++) {
      x[j]++;
    }
  }

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

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

int a[100005] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c]++;a[b+1]--;
	}
	int sum=0;
	int maxn=1;
	for(int i=0;i<=100005;++i){
		sum+=a[i];
		if(i <= sum)
		{
			maxn = i;
		}
	}
	cout << maxn-1 << endl;
	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, i-1);
  }
  cout << mx << endl;
   
  return 0;
}

C++: 
#include <stdio.h>
#include <iostream>
#include <vector>
#include <utility>
#include <math.h>

using namespace std;

const int M = 100001+3;

int solve( int N );

int main(void) {
	
	int N;
	cin >> N;
	cout << solve(N) << endl;
	
	return 0;
}


int solve( const int N ) {

	int ans = 0;

	//?´????????????¨???
	vector< int > v(M,0);

	v[0]++;
	v[v.size()-1]--;
	for( int i = 0; i < N; i++ ) {
		int a, b;
		cin >> a >> b;
		v[a]++;
		v[b+1]--;
	}

	for( int i = 0; i+1 < M; i++ )
		v[i+1] += v[i];

	for( int i = 0; i < M; i++)
		if( min(v[i],i) > ans && (v[i] >= i) )
			ans = min(v[i],i);

	return max(ans-1,0);

}

C++: #include<iostream>
using namespace std;
int n;
int cnt[1<<17];
main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int a,b;cin>>a>>b;
		cnt[a]++,cnt[b+1]--;
	}
	for(int i=1;i<1<<17;i++)cnt[i]+=cnt[i-1];
	int ans=0;
	for(int i=0;i<1<<17;i++)if(cnt[i]+1>=i)ans=ans<i-1?i-1:ans;
	cout<<ans<<endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef pair<int,int> P;

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

int main(){
	cin >> n;
	rep(i,n){
		cin >> a[i] >> b[i];
		a[i]--; b[i]--;
	}
	int s = 0, e = n+1, mid;
	while(e-s > 1){
		mid = (s+e)/2;
		int cnt = 0;
		rep(i,n){
			if(a[i] <= mid && mid <= b[i]) cnt++;
		}
		if(cnt >= mid) s = mid;
		else e = mid;
	}
	cout << s << endl;
}

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

vector<long> a(100002);
vector<long> b(100002);
vector<long> k(100002);
vector<long> s(100002);

int main(){
  int N;
  cin>>N;
  FOR(i,1,N+1){
    cin>>a[i]>>b[i];
  }

  FOR(i,1,100002){
    k[i]=i;}

  for(int i=1;i<100002;i++){
    for(int j=a[i];j<=b[i];j++){
      s[i]++;
    }
  }

  int max_o=0;
  FOR(i,1,100002){
      if(s[i]+1>=k[i]){
	max_o=k[i]-1;
      }
    }

    cout<<max_o<<endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdlib>

using namespace std;

#define ALL(c) c.begin(),c.end()
#define RALL(c) c.rbegin(),c.rend()
#define SORT(x) sort((x).begin(),(x).end())
#define REP(i,x,y) for(int i=(x);i<(y);++i)
#define MP(a,b) make_pair((a),(b))
#define F_ first
#define S_ second
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int lli;
typedef pair<int,int> pii;

const int INF=1<<30;
const long long int INF_=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N;
	cin >> N;
	vector<int> s(100003);
	++s[0];
	REP(i,0,N){
		int a,b;
		cin >> a >> b;
		++s[a];
		--s[b+1];
	}
	int ans=0;
	REP(i,1,100003){
		s[i]+=s[i-1];
		if(i<=s[i]) ans=max(ans,min(i-1,s[i]));
	}
	cout << ans << endl;
	return 0;
}

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

bool data[100001][100];

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

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[i][j] = true;
	}

	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = 0;
		for(int j = 1; j <= n; j++){
			if(data[j][i]) ok++;
		}
		if(ok + 1 >= i) v.push_back(i);
	}

	if(v.size()){
		sort(v.begin(), v.end());
		cout << v[v.size() - 1] - 1 << endl;
	}
	else cout << 0 << endl;

	return 0;
}

C++: #include<iostream>
#include<map>
#include<queue>
#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[i].fs >> p[i].sc;
  p[n] = P(1,100001);
  sort(p,p+n+1);

  priority_queue<int, vector<int>, greater<int> > q;
  int ans = 1;

  for(int i=0;i<=n;i++){
    while(!q.empty() && q.top() < p[i].fs)q.pop();
    q.push(p[i].sc);    
    if(p[i].fs <= q.size() && q.size() <= q.top())ans = max(ans,(int)q.size());
  }

  cout << ans-1 << endl;
}