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
bb=bisect.bisect_left(blist,N+1)
alist=sorted([x[0] for x in tlist[bb:]])
tlist=tlist[:bb]
blist=blist[:bb]
alist=alist[:bisect.bisect(alist,N+1)]
if len(alist)>=N:
    print N
else:
    for k in range(N-1,0,-1):
        bb=bisect.bisect_left(blist,k+1)
        newa=[x[0] for x in tlist[bb:]]
        tlist=tlist[:bb]
        blist=blist[:bb]
        for a in newa:
            bisect.insort_left(alist,a)
        alist=alist[:bisect.bisect(alist,k+1)]
        if len(alist)>=k:
            print k
            break
    else:
        print 0
            