
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)
        

