import bisect

N=int(raw_input())
tlist=[]
for i in range(N):
    [a,b]=[int(x) for x in raw_input().split()]
    tlist.append([a,b])
tlist.sort(key=lambda x:x[1])
blist=[x[1] for x in tlist]
#case N
for k in range(N,0,-1):
    alist=sorted([x[0] for x in tlist[bisect.bisect(blist,k+1):]])
    alist=alist[:bisect.bisect(alist,k+1)]
    if len(alist)==k:
        print k
        break
else:
    print 0
        