#!/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)

