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>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;

struct info {
	int min;
	int max;
	bool flg = true;
};

bool info_first(const info& a, const info& b) {
	return a.min > b.min;
}
bool info_second(const info& a, const info& b) {
	return a.max > b.max;
}

signed main() {
	cin1(n);

	vector<info> P(n);
	for (int i = 0; i < n; i++) {
		cin2(P[i].min,P[i].max);
	}
	
	sort(P.begin(),P.end(),info_first);

	int ans = n + 1;	
	for (int i = 0; i < n; i++) {
		if (P[i].min > ans) {
			ans--;
			P[i].flg = false;
		}
	}
	
	sort(P.begin(),P.end(),info_second);
	for (int i = 0; i < n; i++) {
		//////cout << P[i].min << " " << P[i].max << " " << endl;	
		if (P[i].flg and P[i].max < ans) {
			ans--;
		}
	}

	cout(ans-1);

	return 0;
}

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

int main(){
  int field[100010];
  int N;
  cin >> N;
  memset(field, 0, sizeof(field));
  int a, b;
  for(int i = 0 ; i < N ; i++){
    cin >> a >> b;
    field[a]++;
    field[b]--;
  }
  
  int ans = 0, sum = 0;
  
  for(int i = 1 ; i <= 100001 ; i++){
    sum += field[i];
    if(i <= sum+1) ans = i;
  }
  
  cout << ans-1 << endl;
  return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N; cin >> N;
  int imos[100005]; CLR(imos);
  FOR(i,0,N) {
    int a, b; cin >> a >> b;
    imos[a]++;
    imos[b+1]--;
  }
  int ans = 0;
  FOR(i,0,N+2) {
    imos[i+1] += imos[i];
    if(imos[i] >= i - 1) {
      ans = max(ans, i - 1);
    }
  }
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int main(){
  int ans=0;
  int n,a,b;
  int imos[100005]={0};
  cin>>n;
  rep(i,n){
    cin>>a>>b;
    imos[a]++;
    imos[b+1]--;
    //cout<<imos[a]<<" "<<imos[b+1]<<endl;
  }
  REP(i,1,100){
    imos[i]+=imos[i-1];
    if(imos[i]>=n)ans=imos[i]-1;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=1;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100002];
  lp(i,100002){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+1){
    if(a[i]>=i) ans=i;
  }
  cout<<ans-1<<endl;
  return 0;
    
}

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

int a[100000] = {};

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

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

int main() {

    int n, imos[100003] = {0};

    imos[0] = 1;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int a, b; cin >> a >> b;
        imos[a]++, imos[b + 1]--;
    }

    for(int i = 1; i < 100003; i++) {
        imos[i] += imos[i - 1];
    }

    int maxi = 0;
    for(int i = 0; i <= n; i++) {
        if(imos[i] >= i) maxi = max(maxi, i);
    }
    cout << maxi - 1 << endl;
}

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

int a[100000] = {};

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]--;
	}
	int sum=0;
	int maxn=-1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i <= sum)
		{
			if(maxn<sum)
				maxn = sum;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

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

int fav[100005];

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

	memset(fav, 0, sizeof(fav));
	for(int i=0; i<N; i++)
	{
		int l,r;
		cin >> l >> r;
		fav[l-1]++;
		fav[r]--;
	}

	int res=0, mem=0;
	for(int i=1; i<=N+1; i++) {
		mem+=fav[i];
		if(mem >= i) res = i;
	}

	cout << res << endl;
}

C++: #include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int x[100010];

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