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)