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
}

Python: def appoint(x, l, h):
    if l <= x <= h:
        return 1
    else:
        return 0


N = int(input())
friends = [list(map(int, input().split())) for i in range(N)]
min_group = min(x[0] for x in friends)
max_group = max(x[0] for x in friends)

t = 0
ans = 1
for i in range(min_group, max_group):
    z = sum([appoint(i + 1, lo, hi) for lo, hi in friends])
    if i + 1 == z:
        ans = z
        t = 1
    if t == 1 and i + 1 != z:
        break

print(ans - 1)

Python: N = int(input())
A = [0] * 101010
for i in range(N):
    a, b = map(int, input().split())
    A[a] += 1
    A[b+1] -= 1
ans = 1
cuma = A[0]
for i, a in enumerate(A[1:], 1):
    cuma += a
    if cuma >= i-1:
        ans = i
print(ans-1)

Python: N = int(input())
l = [list(map(int, input().split())) for _ in range(N)]

diff = [0] * 100001
for k in l :
    diff[k[0] - 1] += 1
    diff[k[1]] -= 1

hist = [0,0]
for i in range(2, N + 1) :
    hist.append(hist[-1] + diff[i-1])

print(max([y - 1 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

Python: 

N=int(input())

friendlist=[0 for i in range(N+2)]

"""
for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(b+1,N+2)):
        friendlist[k]+=1
"""


ab=[[int(i) for i in input().split(" ")] for j in range(N)]
a=[ab[i][0] for i in range(N)]
b=[ab[i][1] for i in range(N)]

a.sort()
b.sort()



for i in range(1,N):
    for j in range(a[i-1],min(a[i],N+2)):
        friendlist[j]+=i
for i in range(a[N-1],N+2):
    friendlist[i]+=N

for i in range(1,N):
    for j in range(b[i-1]+1,min(b[i]+1,N+2)):
        friendlist[j]-=j
for i in range(b[N-1]+1,N+2):
    friendlist[i]-=N
        


#"""

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Python: N = int(input())
MAXP = 100010

imos = [0 for i in range(MAXP)]
for i in range(N):
    a,b = map(int,input().split())
    imos[a-1] += 1
    imos[b] -= 1
nums = [0]
for im in imos:
    nums.append(nums[-1] + im)

for i,p in enumerate(reversed(nums)):
    n = MAXP - 1 - i
    if n == 0: break
    if p >= n:
        print(n)
        exit()
print(0)

Python: N = int(input())
R = [0 for i in range(100003)]
for i in range(N):
    a, b = map(int, input().split())
    R[a] += 1
    R[b+1] -= 1
x = 1
y = 1
for i in range(100003):
    x += R[i]
    if x >= i:
        y = max(i, y)
print(y-1)

Python: N=int(input())

friendlist=[0 for i in range(N+1)]

for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(N+1,b+1)):
        friendlist[k]+=1

k=N
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Python: n = int(input())
a = [0] * (10**5 + 2)
for i in range(n) :
    mi, ma = map(int, input().split())
    a[mi-1] += 1
    a[ma] -= 1
for i in range(n) :
    a[i+1] += a[i]
    
ans = 0
for i in range(n, 0, -1) :
    if a[i] >= i :
        ans = i
        break
print(ans)

Python: l=[list(map(int,input().split())) for _ in range(int(input()))]
l=sorted(l,key=lambda x:x[0])
m=[0 for i in range(100002)]
c=1
for i in l:
    if i[0]<=c+1:
        m[i[0]]+=1
        m[i[1]]-=1
        c+=1
for i in range(2,100002):
    m[i]+=m[i-1]
print(max(m))

Python: n = int(input())
l = [0 for i in range(n+1)]
for i in range(n):
  [a,b] = list(map(int,input().split()))
  if a <= n:
    l[a] += 1
    if b <= n:
      l[b] -= 1
ans = 0
for i in range(n+1):
  if i != 0:
    l[i] = l[i-1] + l[i]
  if l[i] >= i-1:
    ans = i-1
print(ans)

Python: N = int(input())
P = []
for i in range(N):
    a, b = map(int, input().split())
    P.append((a, b))

Fmaxim = 0
for i in range(N+1):
    Pcount = 1
    for a, b in P:
        if a <= i+1 <= b:
            Pcount += 1
    if Pcount >= i+1:
        Fmaxim = max(Fmaxim, i)

print(Fmaxim)

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

C++: #include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
typedef pair<int,int> P;
typedef long long ll;

int main(){
    int n; cin >> n;
    vector<ll> num(100200,0);

    rep(i,n){
        int a, b; cin >> a >> b; b++;

        for(int i = a; i < b; i++){
            num[i]++;
        }
    }

    ll ans = 0;
    for(int i = 1; i < 100100; i++){
        int k = i-1;
        if(k <= num[i]) ans = k;
    }

    //imosの方が良かったかも。
    cout << ans << endl;

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
 
using std::cin;
using std::cout;
using std::endl;
 
int main(void) {
    int N;
    cin >> N;
    std::vector<int> frimin(N);
    std::vector<int> frimax(N);
    for (int i = 0; i < N; i++) {
        cin >> frimin[i] >> frimax[i];
    }
 
    std::sort(frimin.begin(), frimin.end());
    std::sort(frimax.begin(), frimax.end());
 
    int ret = 0;
    int count = 1;
    int imin = -1;
    int imax = -1;
    for (int f = 2; f < N+2 && f <= frimax[N-1]; f++) {
        for (int i = imin+1; frimin[i] == f; i++) {
            count++;
            imin++;
        }
        for (int i = imax+1; frimax[i] == f-1; i++) {
            count--;
            imax++;
        }
 
        if ( count >= f) {
            ret = f-1;
        }
    }
 
    cout << ret << endl;
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

using namespace std;

using P = tuple<int,int>;
const int NMAX = 100008;

int main() {
  int N; cin >> N;
  vector<int> vec(NMAX, 0);
  REP(i, N) {
    int A, B; cin >> A >> B;
    ++vec[A - 1];
    --vec[B];
  }
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    vec[i] += vec[i - 1];
    if (vec[i] >= i) {
      ans = max(ans, min(i, vec[i]));
    }
  }
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(){
  int num[100005]={0};
  int n,m=0;
  cin>>n;
  int a,b;
  //num[0]++;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    //for(int j=a;j<=b;j++){
    //num[j]++;
    //}
    num[a]++;
    num[b+1]--;
  }

  for(int i=2;i<10005;i++){
    num[i]+=num[i-1];
    if(num[i]+1>=i)
      cout<<i-1<<endl;
  }
  //for(int i=10004;i>0;i--){
  //if(num[i]>=i){
  //  cout<<i-1<<endl;
    // return 0;
    //}
  //}
}