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: 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

for i in range(1,100001):
    t[i] += t[i-1]
    if (t[i] > i):
        ans = i

print(ans)

Python: N = int(input())
P = []
bmax = 0
for i in range(N):
    a, b = map(int, input().split())
    bmax = max(bmax, b)
    if a <= N+1:
        P.append((a, b))
if len(P) > 0:
    for i in range(min(len(P), bmax), -1, -1):
        Pcount = 1
        for a, b in P:
            if a <= i+1 <= b:
                Pcount += 1
        if Pcount >= i+1:
            print(i)
            break
else:
    print('0')

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]-=i
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: import itertools
n=int(input())
table=[0 for i in range(100003)]
table[0]=1
for i in range(n):
    a,b=map(int,input().split())
    table[a]+=1
    table[b+1]-=1
table=list(itertools.accumulate(table))
for i in range(len(table)):
    if table[i]>=i:
        ans=i
print(ans-1)

Python: n=int(input())
l=[0 for i in range(100003)]
for i in range(n):
    a,b=map(int,input().split())
    l[a]+=1
    l[b+1]-=1
su=1
res=1
for i in range(100003):
    su+=l[i]
    if su>=i:
        res=max(i,res)
print(res-1)

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(100003)]
c=1
for i in l:
    if i[0]<=c+1 and c+1<=i[1]:
        if i[0]!=i[1]:
            m[i[0]]+=1
            m[i[1]]-=1
        else:
            m[i[0]]+=1
        c+=1
for i in range(2,100003):
    m[i]+=m[i-1]
print(max(m))

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n = I()
        aa = [LI() for _ in range(n)]
        t = [0] * 100003
        for a,b in aa:
            t[a] += 1
            t[b+1] -= 1
        s = 0
        r = 0
        for i in range(100002):
            s += t[i]
            if i-1 <= s:
                r = i - 1
        rr.append(r)
        break

    return '\n'.join(map(str, rr))


print(main())

Python: N = int(input())
MAX_N = int(1e5) + 2
acc = [0] * (MAX_N + 1)
acc[0] += 1
acc[MAX_N] -= 1
for _ in range(N):
    a, b = map(int, input().split())
    acc[a] += 1
    acc[b + 1] -= 1
for i in range(MAX_N):
    acc[i + 1] += acc[i]
for i in reversed(range(MAX_N)):
    if acc[i] >= i:
        print(i - 1)
        break

Python: n = int(input())
l = [0 for i in range(n+2)]
for i in range(n):
  [a,b] = list(map(int,input().split()))
  if a <= n+1:
    l[a] += 1
    if b+1 <= n+1:
      l[b+1] -= 1
ans = 0
for i in range(n+2):
  if i != 0:
    l[i] = l[i-1] + l[i]
  if l[i] >= i-1:
    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 + 2) :
    hist.append(hist[-1] + diff[i-1])

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

Python: #!/usr/bin/env python3
n = int(input())

# imos[k]: わたしがちょうどk人誘うときに来れる最大人数
imos = [0] * (10**5 + 2)
for _ in range(n):
    a, b = map(int, input().split())
    imos[a - 1] += 1
    imos[b] -= 1
for i in range(n):
    imos[i + 1] += imos[i]

ans = 0
for i in range(n + 1):
    if imos[i] >= i:
        ans = max(ans, i)
print(ans)

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

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

print(ans)

Python: 
N = int(input())
a = []
b = []

for i in range(N):

    an , bn = map(int,input().split())

    a.append(an)
    b.append(bn)

a.sort()
b.sort()

a.append(1000000)
b.append(1000000)

lis = [0]

for i in range(N+1):

    i += 1
    now = lis[i - 1]

    while a[0] == i:

        now += 1
        del a[0]

    while b[0] == i-1:

        now -= 1
        del b[0]

    lis.append(now)

ans = 0
#print (lis)
for i in range(len(lis)):

    if i <= lis[i]+1 and ans < i:
        ans = i

print (ans - 1)

Python: N = int(input())

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

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

ans = 0

for i in range(1,100001):
    t[i] += t[i-1]
    if (t[i] > i):
        ans = i

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 + 2) :
    hist.append(hist[-1] + diff[i-1])

print(max([0] + [y - 1 for (x,y) in zip(hist, range(N + 2)) 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+1)):
        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())
import sys
nn = [0 for _ in range(10**5+3)]
for _ in range(N):
  a,b = map(int,input().split())
  nn[a] += 1
  nn[b+1] -= 1
ans = 0
sss = 1
for i in range(len(nn)):
  sss += nn[i]
  if i <= sss:
    ans = i
print(ans-1)

Python: N = int(input())
P = []
for i in range(N):
    a, b = map(int, input().split())
    if a <= N+1:
        P.append((a, b))
if len(P) > 0:
    for i in range(len(P), -1, -1):
        Pcount = 1
        for a, b in P:
            if a <= i+1 <= b:
                Pcount += 1
        if Pcount >= i+1:
            print(i)
            break
else:
    print('0')

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(N+2,b+1)):
        friendlist[k]+=1

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

print(i)