You are given a rooted tree with vertices numerated from $1$ to $n$. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.

Ancestors of the vertex $i$ are all vertices on the path from the root to the vertex $i$, except the vertex $i$ itself. The parent of the vertex $i$ is the nearest to the vertex $i$ ancestor of $i$. Each vertex is a child of its parent. In the given tree the parent of the vertex $i$ is the vertex $p_i$. For the root, the value $p_i$ is $-1$.

 [Image] An example of a tree with $n=8$, the root is vertex $5$. The parent of the vertex $2$ is vertex $3$, the parent of the vertex $1$ is vertex $5$. The ancestors of the vertex $6$ are vertices $4$ and $5$, the ancestors of the vertex $7$ are vertices $8$, $3$ and $5$ 

You noticed that some vertices do not respect others. In particular, if $c_i = 1$, then the vertex $i$ does not respect any of its ancestors, and if $c_i = 0$, it respects all of them.

You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex $v$, all children of $v$ become connected with the parent of $v$.

 [Image] An example of deletion of the vertex $7$. 

Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of vertices in the tree.

The next $n$ lines describe the tree: the $i$-th line contains two integers $p_i$ and $c_i$ ($1 \le p_i \le n$, $0 \le c_i \le 1$), where $p_i$ is the parent of the vertex $i$, and $c_i = 0$, if the vertex $i$ respects its parents, and $c_i = 1$, if the vertex $i$ does not respect any of its parents. The root of the tree has $-1$ instead of the parent index, also, $c_i=0$ for the root. It is guaranteed that the values $p_i$ define a rooted tree with $n$ vertices.


-----Output-----

In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer $-1$.


-----Examples-----
Input
5
3 1
1 1
-1 0
2 1
3 0

Output
1 2 4 

Input
5
-1 0
1 1
1 1
2 0
3 0

Output
-1

Input
8
2 1
-1 0
1 0
1 1
1 1
4 0
5 1
7 0

Output
5 



-----Note-----

The deletion process in the first example is as follows (see the picture below, the vertices with $c_i=1$ are in yellow):

  first you will delete the vertex $1$, because it does not respect ancestors and all its children (the vertex $2$) do not respect it, and $1$ is the smallest index among such vertices;  the vertex $2$ will be connected with the vertex $3$ after deletion;  then you will delete the vertex $2$, because it does not respect ancestors and all its children (the only vertex $4$) do not respect it;  the vertex $4$ will be connected with the vertex $3$;  then you will delete the vertex $4$, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth);  you will just delete the vertex $4$;  there are no more vertices to delete. 

 [Image] 

In the second example you don't need to delete any vertex:

  vertices $2$ and $3$ have children that respect them;  vertices $4$ and $5$ respect ancestors. 

 [Image] 

In the third example the tree will change this way:

 [Image]

Difficulty: interview

Starter code:

Solutions:
n = int(input())
parent = [-1]*n
isresp = [1]*n
for i in range(n):
	p, r = list(map(int, input().split()))
	p -= 1
	if r == 0:
		isresp[i] = 0
		if p>=0:
			isresp[p] = 0
nore = []
for i in range(n):
	if isresp[i] == 1:
		nore.append(i+1)
if not nore:
	print(-1)
else:
	print(" ".join(map(str, nore)))


n = int(input())

tree  = [[] for _ in range(n)]
inf = [0] * n

for i in range(n):
    ind, f = map(int, input().split())
    inf[i] = f
    if ind != -1:
        tree[ind - 1].append(i)

flag = False

for i in range(n):
    if inf[i] == 1:
        for x in tree[i]:
            if inf[x] != 1:
                break
        else:
            flag = True
            print(i + 1, end=' ')

if not flag:
    print(-1)

import sys

n = int(sys.stdin.readline().strip())
R = [0] * n

for i in range (0, n):
    line = sys.stdin.readline().strip().split()
    p = int(line[0])
    c = int(line[1])
    if c == 0:
        R[i] = 1
        if p != -1:
            R[p - 1] = 1

ans = [0] * (n - sum(R))
j = 0
for i in range (0, n):
    if R[i] == 0:
        ans[j] = str(i + 1)
        j = j + 1
ans = " ".join(ans)

if ans == "":
    print(-1)
else:
    print(ans)

a=int(input())
A = [0]*(a+2)
B = [0]*a
for i in range(a):
    q,w = map(int,input().split())
    A[i]+=1
    A[q-1]+=1
    if w == 1:
        B[q-1]+=1
        B[i]+=1
C = []
for i in range(a):
    if A[i] == B[i]:
        C.append(i+1)
if len(C)==0:
    print(-1)
else:
    print(*C)

n = int(input())
lis = []
for _ in range(n):
    lis.append(list(map(int, input().split())))
    
x = list([0]*n)
p = list([0]*n)

res = []
for i in lis:
    if i[0] != -1:
        p[i[0]-1] += (i[-1])
        x[i[0]-1] += 1    
for i in range(n):
    if lis[i][-1] == 1:
        if x[i] == p[i]:
            res.append(i+1)

if len(res) == 0:
    print(-1)
else:
    print(" ".join(map(str, sorted(res))))


from collections import defaultdict


n = int(input())
g = defaultdict(list)
cs = [None] * n

for v, _ in enumerate(range(n)):
    p, c = map(int, input().split())
    if p != -1:
        g[p - 1].append(v)
    cs[v] = c

r = []
used = [False] * n

def dfs(g, stack):
    nonlocal r, used
    while stack:
        v = stack.pop()
        used[v] = True

        flag = True if cs[v] == 1 else False
        for u in g[v]:
            if not used[u]:
                stack.append(u)
            if cs[u] == 0:
                flag = False

        if flag:
            r.append(v)


for v in range(n):
    if not used[v]:
        dfs(g, [v])

if r:
    for el in sorted(r):
        print(el + 1, end=" ")
else:
    print(-1)


import sys
input = sys.stdin.readline

n=int(input())
EDGE=[list(map(int,input().split())) for i in range(n)]

CHILDLIST=[[] for i in range(n+1)]


for i in range(n):
    if EDGE[i][0]==-1:
        continue
    CHILDLIST[EDGE[i][0]].append(i+1)

ANS=[]

for i in range(1,n+1):
    if EDGE[i-1][1]==1:

        
        for chi in CHILDLIST[i]:
            if EDGE[chi-1][1]==0:
                break

        else:
            ANS.append(i)

ANS.sort()
if ANS==[]:
    print(-1)
print(*ANS)


import sys
N = int(input())
Er = set(range(1, N+1))
for i in range(1, N + 1):
    p, c = map(int, sys.stdin.readline().split())
    if c == 0:
        if i in Er:
            Er.remove(i)
        if p in Er:
            Er.remove(p)
Er = list(Er)
if not Er:
    print(-1)
else:
    Er.sort()
    print(*Er)

from collections import defaultdict as dd
import math
import heapq
def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return list(map(int, input().split()))

def lm():
	return list(map(int, input().split()))

n=nn()


parentlist=[]


for i in range(n):
	parentlist.append(lm())




childlist=dd(list)

for i in range(1,n+1):
	childlist[i]=[]



for vertex, pair in enumerate(parentlist):
	#print(vertex,pair)
	#print(childlist)
	parent=pair[0]
	#print(parent)
	if not parent==-1:
		#print(childlist[parent])
		(childlist[parent]).append((vertex+1,pair[1]))

blist=[]

for vertex in childlist:
	if childlist[vertex]==[] or all([child[1]==1 for child in childlist[vertex]]):
		if parentlist[vertex-1][1]==1:
			blist.append(vertex)

deleted=sorted(blist)


if len(deleted)==0:
	print(-1)

else:
	print(*deleted)
	










#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#

"""
549 C
"""

n = int(input())
d = [1 for i in range(n+1)]

for i in range(1, n+1):
    pi, ci = list(map(int, input().split()))
    if ci == 0:
        d[i] = 0
        if pi != -1:
            d[pi] = 0

e = []
for i in range(1, n+1):
    if d[i] == 1:
        e.append(i)

if len(e) == 0:
    print(-1)
    quit()
else:
    print(" ".join(map(str, e)))


n = int(input().strip())

parent = [None for i in range(n+1)]
children = [[] for i in range(n+1)]
cs = [None for i in range(n+1)]

for i in range(1, n+1):
	p, c = list(map(int, input().split()))
	parent[i] = p
	if p != -1:
		children[p].append(i)
	cs[i] = c

# print(parent)
# print(children)
# print(cs)

to_delete = []
for i in range(1, n+1):
	if parent[i] > 0 and cs[i] == 1 and sum([cs[item] for item in children[i]]) == len(children[i]):
		to_delete.append(i)

if len(to_delete) > 0:
	print(' '.join([str(item) for item in to_delete]))
else:
	print(-1)


3

def ir(): return int(input())
def ia(): return [int(i) for i in input().split()]

n = ir()

p = [None] * n
c = [None] * n
adj =  [ [ ] for i in range(n) ]

good = [False] * n

for i in range(n):
	p0, c0 = ia()
	c[i] = c0
	if p0 == -1:
		p[i] = p0
		good[i] = True
	else:
		p0 -= 1
		p[i] = p0
		adj[p0].append(i)
		if c0 == 0:
			good[i] = True
			good[p0] = True

ans = [ ]
for i, e in enumerate(good):
	if not e:
		ans.append(i)

ans.sort()

if ans == []:
	print(-1)
else:
	ans = [str(e + 1) for e in ans]
	print( " ".join(ans) )

n = int(input())
mass = [0 for i in range(n + 10)]
arr = [0 for i in range(n + 10)]
nnn = [0 for i in range(n + 10)]
for i in range(n):
    a, b = map(int, input().split())
    if a != -1:
        mass[a] += 1
        arr[a] += b
    nnn[i + 1] = b
ans = []
for i in range(n):
    if mass[i + 1] == arr[i + 1] and nnn[i + 1] == 1:
        ans.append(i + 1)
if len(ans) == 0:
    ans.append(-1)
for i in ans:
    print(i, end = ' ')

'''input
5
-1 0
1 1
1 1
2 0
3 0
'''
from sys import stdin, stdout
import sys
from collections import defaultdict, deque

sys.setrecursionlimit(15000)


def bfs(graph, parent, n):
	myq = deque()
	myq.append([parent, 0])
	ans = []
	while len(myq) != 0:
		node = myq.popleft()
		flag = 0
		for i in graph[node[0]]:
			myq.append(i)
			if i[1] == 1:
				continue
			else:
				flag = 1
		if flag == 0 and node[1] == 1:
			ans.append(node[0])
	return ans

# main starts
n  = int(stdin.readline().strip())
graph = defaultdict(list)
parent = -1
for i in range(1, n + 1):
	node, key = list(map(int, stdin.readline().split()))
	if node == -1:
		parent = i
		continue		
	graph[node].append([i, key])
#print(graph)
ans = bfs(graph, parent, n)
ans.sort()
if len(ans) > 0:
	print(*ans)
else:
	print(-1)

class node:
    def __init__(self, value, parent, respect):
        self.value = value
        self.children = []
        self.parent = parent
        self.respect = respect

all_nodes = {}
root = -1

not_respectors = []

import sys

n = int(sys.stdin.readline())
for i in range(n):
    line = sys.stdin.readline()[:-1]
    node_parent, node_res = line.split(" ")
    if i+1 in all_nodes:
        all_nodes[i+1].parent = int(node_parent)
        all_nodes[i+1].respect = int(node_res)
    else:
        all_nodes[i+1] = node(i+1, int(node_parent), int(node_res))

    if int(node_parent) == -1:
        root = all_nodes[i+1]
    else:
        int_node_parent = int(node_parent)
        if int_node_parent not in all_nodes:
            all_nodes[int_node_parent] = node(int_node_parent, -1, -1)
        all_nodes[int_node_parent].children.append(all_nodes[i+1])

    if int(node_res) == 1:
        not_respectors.append(all_nodes[i+1])

def get_val(n):
    return n.value


not_respecters = sorted(not_respectors, key=get_val)
tot = []
for n in not_respectors:
    b = True
    for c in n.children:
        if c.respect == 0:
            b = False
            break
    if b:
        tot.append(n)


if len(tot) == 0:
    print("-1")
else:
    print(" ".join(map(str, list(map(get_val, tot)))))


n = int(input())
p = [0]
c = [0]
a = []
m = dict()
for i in range(n+1):
	m[i] = []
for i in range(n):
	x,y = list(map(int,input().split()))
	p.append(x)
	c.append(y)
	if x!=-1:
		m[x].append(i+1)
for i in range(1,n+1):
	if c[i]==1:
		if len(m[i])==0:
			a.append(i)
		else:
			for j in m[i]:
				if c[j]==0:
					break
			else:
				a.append(i)

if len(a)==0:
	print(-1)
else:
	print(*sorted(a))


def bs (arr, l, r, x): 
  
    # Check base case 
    if r >= l: 
  
        mid = l + (r - l)//2
  
        # If element is present at the middle itself 
        if arr[mid] == x: 
            return 1 
          
        # If element is smaller than mid, then it can only 
        # be present in left subarray 
        elif arr[mid] > x: 
            return bs(arr, l, mid-1, x) 
  
        # Else the element can only be present in right subarray 
        else: 
            return bs(arr, mid+1, r, x) 
  
    else: 
        # Element is not present in the array 
        return 0

n=int(input())
nrp=[]
rc=[]
for i in range(n):
    p,c=map(int,input().split())
    if c:nrp.append(i+1)
    if not c:rc.append(p)
s=[]
rc.sort()
for i in nrp:
    if not bs(rc,0,len(rc)-1,i):s.append(i)
if not s:print("-1")
else:print(*sorted(s))

n = int(input())
parent_respect = [0] * (n + 1)
graph = [[] for i in range(n + 1)]
root = None
for i in range(1, n + 1):
    p, r = [int(j) for j in input().split()]
    if p == -1:
        root = i
        continue
    graph[p].append(i)
    parent_respect[i] = r

# print(graph)
result = []

for i in range(1, n + 1):
    if parent_respect[i] == 1:
        all_c_r = False
        for c in graph[i]:
            if parent_respect[c] == 0:
                all_c_r = True

        if not all_c_r:
            result.append(i)

if result:
    for i in result:
        print(i, end=' ')
else:
    print(-1)


v = int(input())

need_check = {}
tree = {}

kids = {}

count = 1
while count <= v:
    parent, to_check = list(map(int, input().split()))
    node = count

    tree[node] = parent

    if to_check:
        need_check[node] = 1

    if parent not in kids:
        kids[parent] = []

    kids[parent].append(node)

    count += 1

deletion = []
for node in sorted(need_check.keys()):
    # determine if we'll delete him
    if node in kids and kids[node]:
        to_delete = True

        for kid in kids[node]:
            if kid not in need_check:
                to_delete = False
                break

        if not to_delete:
            continue

        next_parent = tree[node]
        for kid in kids[node]:
            tree[kid] = next_parent

        del kids[node]
    deletion.append(node)

if not deletion:
    print('-1')
else:
    print(' '.join(map(str, deletion)))


import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
# sys.stdin.readline()
from copy import deepcopy


def __starting_point():

    # single variables
    n = [int(val) for val in sys.stdin.readline().split()][0]

    # On each step you select such a non-root vertex that it does not 
    # respect its parent and none of its children respects it

    tree = {i+1 : [[], []] for i in range(n)}
    for i in range(n):
        p, c = [int(val) for val in sys.stdin.readline().split()]
            
        tree[i+1][0].append((p, c))
        if(p != -1):
            tree[p][1].append((i, c))

    d = []
    for i in range(1, n+1):
        p, c = tree[i]
        if(p[0][1] == 1 and sum([x[1] for x in c]) == len(c)):
            d.append(i)

   
    if(len(d) == 0):
        print(-1)
    else: 
        for val in d:
            print(val, end=' ')
        print('')




__starting_point()

n = int(input())

respectingChildren = [0 for i in range(n)]
totalChildren = [0 for i in range(n)]
Ci = []
Pi = []
root = 0

for i in range(n):
    pi, ci = list(map(int, input().split()))
    Ci.append(ci)
    if pi==-1:
        Pi.append(pi)
        root = i
        continue

    Pi.append(pi-1)
    totalChildren[pi-1] += 1
    if ci==0:
        respectingChildren[pi-1] += 1

ans = []
for i in range(n):
    if i==root:
        continue

    if Ci[i]==1:
        if respectingChildren[i]==0:
            ans.append(i+1)
            parentOfI = Pi[i]
            totalChildren[parentOfI] += totalChildren[i]

if ans==[]:
    print(-1)
else:
    print(*ans)