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

