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: from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())



N = inp()
MAX = 100005
imos = [0]*MAX
for _ in range(N):
    a,b = inpl()
    imos[a] += 1
    imos[b+1] -= 1

for i in range(1,MAX):
    imos[i] += imos[i-1]

#print(imos[:10])
ans = 0
for i in range(MAX):
    if imos[i] >= i-1:
        ans = max(ans,i-1)
print(ans)

Python: from itertools import accumulate
n = int(input())
lst = [0] * 100002
for _ in range(n):
  a, b = map(int, input().split())
  lst[a - 1] += 1
  lst[b] -= 1
lst = list(accumulate(lst))
for i in range(n, -1, -1):
  if i <= lst[i]:
    print(i)
    break

Python: def main():
    N = int(input())
    ab = [list(map(int, input().split())) for i in range(N)]

    imos = [0]*(N+3)
    for a, b in ab:
        imos[min(a,N+2)] += 1
        imos[min(b+1,N+2)] -= 1
    for i in range(N+1):
        imos[i+1] += imos[i]
    #print(imos)

    ans = 0
    for i, imo in enumerate(imos):
        if (imo+1 >= i):
            ans = i-1
    print(ans)

main()

Python: N = int(input())
a=[]
p = 0
for i in range(N):
    q=[int(j) for j in input().split()]
    a.append([q[0]-1,q[1]])
    p = max(p, q[1])

table=[0]*(p+1)
for i in range(len(a)):
    table[a[i][0]]+=1
    table[a[i][1]]-=1

for i in range(1,p+1):
    table[i]+=table[i-1]

ans=0
for i, val in enumerate(table):
    if val >= i and i <= N:
        ans = max(i, ans)

print(ans)

Python: import heapq
n=int(input())
abs=sorted([list(map(int,input().split())) for _ in range(n)])
s=0
best=0
heap=[]
for k in range(1,n+1):
    while(s < n and abs[s][0] <= k+1):
        heapq.heappush(heap, abs[s][1])
        s+=1
    while(len(heap) > 0 and heap[0] <= k):
        heapq.heappop(heap)
    if(len(heap) >= k):
        best = k
print(best)

Python: import sys

class Imos1D:
    def __init__(self,sz):
        self.__seq = [0] * sz

    def update(self, a, b): # [a,b)
        self.__seq[a] += 1
        self.__seq[b] -= 1

    def calc(self):
        for i in range(len(self.__seq) - 1):
            self.__seq[i + 1] += self.__seq[i]

    def get(self,i):
        return self.__seq[i]

def main():
    n = int(input())
    imos = Imos1D(1000000 + 10)

    for i in range(n):
        a, b = map(int, sys.stdin.readline().split())
        imos.update(a, b + 1)

    imos.calc()

    # solve
    ans = 1
    for i in range(n + 2):
        if imos.get(i) >= i - 1:
            ans = max(ans, i)

    print(ans - 1) # ???????????????


if __name__ == '__main__':
    main()

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 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

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,100001):
    m[i]+=m[i-1]
print(max(m))

Python: import heapq
n=int(input())
abs=sorted([list(map(int,input().split())) for _ in range(n)])
s=0
best=0
heap=[]
for k in range(1,n+1):
    while(s < n and abs[s][0] <= k+1):
        heapq.heappush(heap, -abs[s][1])
        s+=1
    while(len(heap) > 0 and -heap[0] <= k):
        heapq.heappop(heap)
    if(len(heap) >= k):
        best = k
print(best)

Python: n=int(input())
l=[]
q=0
for i in range(n):
    p=[int(j) for j in input().split()]
    q=max(q,p[1]+1)
    l.append([p[0],p[1]+1])
a=[]
table=[0]*(q+1)
for i in range(n):
    table[l[i][0]]+=1
    table[l[i][1]]-=1
for i in range(1,q+1):
    table[i]+=table[i-1]
ans=0
for i in range(len(table)):
    if i<=table[i]+1 and i<=n+1:
        ans=max(ans,i)



print(ans-1)

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)]

#print(ab)

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())
arr = [0]*(10**6+2)
for _ in range(n):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    b = min(b,n)
    arr[a] += 1
    if b+1<=n:
        arr[b+1] -= 1
for i in range(n):
    arr[i+1] += arr[i]
res = -float('inf')
for i,j in enumerate(arr[:n+1][::-1]):
    if (n-i)<=j:
        res = max(res,n-i)
print(max(res,0))

Python: n = int(input())
li = [0] * 100003
ans = 0

for i in range(n):
    a,b = map(int,input().split())
    li[a] += 1
    li[b + 1] -= 1

for i in range(1,len(li)):
    li[i] += li[i-1]
    if i -1 <= li[i]:
        ans = i-1
print(ans)

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

diff = [0] * 100002
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([0] + [y - 1 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

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([0] + [y - 1 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

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 p-1 >= n and n >= 0:
        print(n)
        exit()
print(0)

Python: N = int(input())

t = [0 for i in range(100002)]

for i in range(N):
    a,b = [int(i) for i in input().split(' ')]
    t[a-1] +=1
    t[b] -= 1

ans = 0
#print(t)
for i in range(1,100002):
    t[i] += t[i-1]
    if (t[i] >= i):
        ans = i
#print(t)
print(ans)

Python: n=int(input())
t=[0]*1000002

for i in range(n):
    a,b=map(int,input().split())
    t[a-1]+=1
    t[b]-=1
    
ans=0
for i in range(1000002):
    t[i]+=t[i-1]
    if i<=t[i]:
        ans=i

print(ans)

Python: #!/usr/bin/env python3
n = int(input())
li = [0] * (10**5 + 3)
for _ in range(n):
    a, b = map(int, input().split())
    li[a] += 1
    li[b + 1] -= 1
for i in range(n + 1):
    li[i + 1] += li[i]
ans = 0
for i in range(1, n + 2):
    if i - 1 <= li[i]:
        ans = max(ans, i - 1)
print(ans)