n=int(input()) c=[tuple(map(int,input().split())) for i in range(n)] m=[n]*n for i in reversed(range(n)): a,b=c[i] if b==1: m[i]=i if i+1