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: MEDIUM
Tags: Tree algorithms

Graph traversal

Starter code:

Solutions:
import sys, math
n = int(input())
a = [0 for _ in range(n)]
b = [1 for _ in range(n)]
k = -1
for i in range(n):
	(p, c) = map(int, sys.stdin.readline().split())
	if p == -1:
		k = i
	elif c == 0:
		a[p - 1] = 1
	else:
		b[i] = 0
ans = []
for i in range(n):
	if a[i] == 0 and b[i] == 0 and (i != k):
		ans.append(i + 1)
if len(ans) == 0:
	print(-1)
else:
	print(*ans)


def all_children_disrespect(i, tree):
	return tree[i][2] == tree[i][4]
n = int(input())
tree = [[0, 0, 0, 0, 0] for i in range(n + 1)]
for i in range(n):
	(parent, c) = map(int, input().split())
	tree[i + 1][0] = parent
	tree[i + 1][1] = c
	if parent != -1:
		if c == 0:
			tree[parent][3] += 1
		else:
			tree[parent][4] += 1
		tree[parent][2] += 1
disrespectful = list(filter(lambda x: x[1] == 1, tree))
delet = []
for i in range(1, len(tree)):
	if tree[i][1] == 1 and all_children_disrespect(i, tree):
		delet.append(i)
if delet:
	delet = map(lambda x: str(x), delet)
	print(' '.join(delet))
else:
	print(-1)


n = int(input())
tree = [[] for _ in range(n)]
p = [0] * n
c = [0] * n
for i in range(n):
	(p[i], c[i]) = map(int, input().split())
	if p[i] == -1:
		continue
	p[i] -= 1
	tree[p[i]].append(i)
ans = []
for i in range(n):
	if c[i] and all((c[j] for j in tree[i])):
		ans.append(i + 1)
if ans:
	print(*ans)
else:
	print(-1)


n = int(input())
parent = [0 for i in range(n)]
root = [0 for i in range(n)]
respected_parent = [0 for i in range(n)]
do_not_respect_child = [0 for i in range(n)]
for i in range(n):
	(p, c) = map(int, input().split())
	parent[i] = p
	if p == -1:
		root[i] = 1
	if c == 0 and p != -1:
		respected_parent[p - 1] = 1
	else:
		do_not_respect_child[i] = 1
ls = []
for i in range(n):
	if respected_parent[i] == 0 and do_not_respect_child[i] == 1 and (root[i] == 0):
		ls.append(i + 1)
if len(ls) > 0:
	print(*ls)
else:
	print(-1)


n = int(input())
p = [-1] * n
c = [-1] * n
d = [False] * n
for i in range(n):
	(p[i], c[i]) = list(map(int, input().split()))
for i in range(n):
	if p[i] == -1:
		continue
	if c[i] == 0:
		d[i] = True
		d[p[i] - 1] = True
flag = False
o = []
for i in range(n):
	if p[i] == -1:
		continue
	if not d[i]:
		flag = True
		o.append(i + 1)
if not flag:
	print(-1)
else:
	print(' '.join((str(x) for x in o)))


array = []
q = int(input())
for _ in range(q):
	(p, i) = map(int, input().split())
	array.append((p, i))
new_array = [1] * q
for ef in range(q):
	check = array[ef][1]
	parent = array[ef][0]
	if check == 0:
		if parent != -1:
			new_array[parent - 1] = 0
		new_array[ef] = 0
result = []
for (i, j) in enumerate(new_array):
	if j == 1:
		result.append(i + 1)
if result == []:
	print(-1)
else:
	print(' '.join(map(str, result)))


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
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)


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

def __starting_point():
	n = [int(val) for val in sys.stdin.readline().split()][0]
	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())
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)


n = int(input())
a = []
a.append(0)
for i in range(n):
	a.append(0)
f = []
f.append(0)
for i in range(n):
	(b, c) = map(int, input().split())
	if c == 0 and b != -1:
		a[b] = 1
	f.append(c)
t = 0
for i in range(1, n + 1):
	if a[i] == 0 and f[i] == 1:
		print(i, end=' ')
		t = 1
if t == 0:
	print(-1)


a = int(input())
p = {}
c = {}
for i in range(1, a + 1):
	(x, y) = map(int, input().split())
	c[i] = y
	p[i] = x
ans = c.copy()
for i in range(1, a + 1):
	if p[i] != -1 and c[i] == 0:
		ans[p[i]] = 0
x = 0
for i in range(1, a + 1):
	if ans[i]:
		print(i, end=' ')
		x += 1
if x == 0:
	print(-1)


n = int(input())
tree = [[1, 1] for i in range(n)]
for i in range(n):
	(p, c) = map(int, input().split())
	p -= 1
	if c == 0:
		tree[i][0] = 0
	if p > -1:
		tree[p][1] = min(c, tree[p][1])
flag = True
for i in range(n):
	if sum(tree[i]) == 2:
		flag = False
		print(i + 1, end=' ')
if flag:
	print(-1)


from collections import defaultdict
d = defaultdict(list)

def fun(value, d):
	child = [None] * (n + 1)
	for i in range(1, n + 1):
		children = d[i]
		if d[i]:
			for v in d[i]:
				if value[v] == 0:
					child[i] = False
					break
			else:
				child[i] = True
		else:
			child[i] = True
	x = False
	for i in range(1, n + 1):
		if value[i] == 1 and child[i]:
			x = True
			print(i, end=' ')
	if x == False:
		print(-1)
n = int(input())
value = [None] * (n + 1)
for i in range(1, n + 1):
	(p, v) = map(int, input().split())
	d[p].append(i)
	value[i] = v
fun(value, d)


n = int(input())
(p, r) = ([0] * n, [0] * n)
for i in range(n):
	(p[i], r[i]) = map(int, input().split())
d = r[:]
for i in range(n):
	if p[i] != -1 and (not r[i]):
		d[p[i] - 1] = 0
if not any(d):
	print(-1)
else:
	print(' '.join((str(i + 1) for (i, v) in enumerate(d) if v)))


n = int(input())
a = [0] * (n + 1)
gr = [0]
for i in range(n):
	(x, y) = map(int, input().split())
	if x != -1 and y == 0:
		a[x] = 1
	gr.append(y)
ans = []
for i in range(1, n + 1):
	if a[i] == 0 and gr[i] == 1:
		ans.append(i)
if len(ans) == 0:
	print(-1)
	exit()
print(*ans)


import os
import sys
from io import BytesIO, IOBase
(sys.stdout, stream) = (IOBase(), BytesIO())
sys.stdout.flush = lambda : os.write(1, stream.getvalue()) and (not stream.truncate(0)) and stream.seek(0)
sys.stdout.write = lambda s: stream.write(s.encode())
zero = 0
numbers = []
(num, sign) = (zero, True)
for char in os.read(0, os.fstat(0).st_size):
	if char >= 48:
		num = num * 10 + char - 48
	elif char == 45:
		sign = False
	elif char != 13:
		numbers.append(num if sign else -num)
		(num, sign) = (zero, True)
if char >= 48:
	numbers.append(num if sign else -num)
getnum = iter(numbers).__next__

def main():
	n = getnum()
	c = [0] * (n + 1)
	tree = [[] for _ in range(n + 1)]
	for i in range(n):
		(p, c[i + 1]) = (max(getnum(), 0), getnum())
		tree[p].append(i + 1)
	res = []
	for i in range(n):
		if c[i + 1] and all((c[j] for j in tree[i + 1])):
			res.append(i + 1)
	if res:
		print(*res)
	else:
		print(-1)
main()


import sys
import math
import heapq
import bisect
from collections import defaultdict as dd

def ch(s):
	if val[s] == 0:
		return 0
	for i in g[s]:
		if val[i] == 0:
			return 0
	return 1
g = dd(list)
val = dd(int)
n = int(input())
for i in range(1, n + 1):
	(f, v) = [int(j) for j in input().split()]
	if f == -1:
		root = i
		val[i] = v
	else:
		g[f].append(i)
		val[i] = v
c = []
for i in range(1, n + 1):
	if ch(i):
		c.append(i)
if len(c) == 0:
	print(-1)
else:
	print(*sorted(c))


n = int(input())
parent = []
respect = []
for i in range(n):
	(p, q) = map(int, input().split(' '))
	parent.append(p - 1)
	respect.append(q)
possibility = []
for i in range(n):
	if respect[i] == 1:
		possibility.append(True)
	else:
		possibility.append(False)
for i in range(n):
	if parent[i] != -2 and respect[i] == 0:
		possibility[parent[i]] = False
flag = False
for i in range(n):
	if possibility[i]:
		flag = True
		print(i + 1, end=' ')
if not flag:
	print(-1)


from collections import defaultdict as dd
import math
import heapq

def nn():
	return int(input())

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

def mi():
	return 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):
	parent = pair[0]
	if not parent == -1:
		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)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
kk = lambda : map(int, input().split())
k2 = lambda : map(lambda x: int(x) - 1, input().split())
ll = lambda : list(kk())
n = int(input())
if n < 3:
	print(-1)
	exit()
parents = [-1] * n
children = [[] for _ in range(n)]
ok = [-1] * n
pos = [True] * n
for node in range(n):
	(p, c) = k2()
	parents[node] = p
	children[p].append(node)
	ok[node] = c + 1
	if c == -1:
		pos[node] = pos[p if p != -2 else node] = False
tbp = []
for node in range(n):
	if pos[node]:
		tbp.append(node + 1)
print(-1 if len(tbp) == 0 else ' '.join(map(str, tbp)))


import sys
input = sys.stdin.readline
from collections import deque

def bfs():
	q = deque([root])
	visited = [False] * n
	visited[root] = True
	res = []
	while q:
		v = q.popleft()
		flag = c[v]
		for nv in G[v]:
			if visited[nv]:
				continue
			flag &= c[nv]
			q.append(nv)
			visited[nv] = True
		if flag:
			res.append(v)
	return res
n = int(input())
G = [[] for _ in range(n)]
c = [False] * n
for i in range(n):
	(pi, ci) = map(int, input().split())
	if pi == -1:
		root = i
	else:
		G[i].append(pi - 1)
		G[pi - 1].append(i)
	if ci == 1:
		c[i] = True
res = bfs()
res.sort()
if len(res) == 0:
	print(-1)
else:
	print(*list(map(lambda x: x + 1, res)))


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil
mod = 10 ** 9 + 7
for _ in range(1):
	n = int(input())
	tree = {i + 1: [] for i in range(n)}
	bad = [0] * (n + 1)
	for i in range(n):
		(x, y) = map(int, input().split())
		if x == -1:
			root = i + 1
		else:
			tree[i + 1].append(x)
		bad[i + 1] = y
	possible = [True for i in range(n + 1)]
	for (key, value) in tree.items():
		if bad[key] == 0:
			possible[key] = False
			for node in value:
				possible[node] = False
	ans = []
	for i in range(1, n + 1):
		if possible[i]:
			ans.append(i)
	if not ans:
		print(-1)
	else:
		print(*ans)


n = int(input())
li = [True] * (n + 1)
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	if c == 0:
		li[i] = False
		li[max(p, 0)] = False
printed = False
for i in range(1, n + 1):
	if li[i]:
		print(i, end=' ')
		printed = True
if not printed:
	print(-1)


n = int(input())
fa = [-1 for i in range(n + 1)]
flag = [0 for i in range(n + 1)]
resp = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	(fa[i], flag[i]) = (int(x) for x in input().split())
	if fa[i] != -1 and (not flag[i]):
		resp[fa[i]] += 1
ans = []
for i in range(1, n + 1):
	if flag[i] and resp[i] == 0:
		ans.append(str(i))
if ans:
	print(' '.join(ans))
else:
	print(-1)


import os
import sys
from io import BytesIO, IOBase
(sys.stdout, stream) = (IOBase(), BytesIO())
sys.stdout.flush = lambda : os.write(1, stream.getvalue()) and (not stream.truncate(0)) and stream.seek(0)
sys.stdout.write = lambda s: stream.write(s.encode())
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
	n = int(input())
	tree = [[] for _ in range(n + 1)]
	p = [0] * (n + 1)
	c = [0] * (n + 1)
	for i in range(n):
		(p[i + 1], c[i + 1]) = map(int, input().split())
		p[i + 1] = max(p[i + 1], 0)
		tree[p[i + 1]].append(i + 1)
	res = []
	for i in range(1, n + 1):
		if c[i] and all((c[j] for j in tree[i])):
			res.append(i)
	if res:
		print(*res)
	else:
		print(-1)
main()


import sys
from functools import reduce
from collections import defaultdict

def remove_nodes(nodes):
	parents = defaultdict(list)
	children = defaultdict(list)
	norespect = defaultdict(list)
	root = -1
	for i in range(0, len(nodes)):
		(parent, respect) = nodes[i]
		if int(parent, 10) == -1:
			root = i + 1
			continue
		parents[i + 1] = int(parent, 10)
		children[int(parent, 10)].append(i + 1)
	for i in range(0, len(nodes)):
		(parent, respect) = nodes[i]
		if int(respect, 10) == 1:
			norespect[i + 1].append(parents[i + 1])
	total_removed = 0
	for i in sorted(norespect.keys()):
		remove = False
		if len(children[i]) == 0:
			remove = True
		else:
			for c in children[i]:
				if c in norespect:
					remove = True
				else:
					remove = False
					break
		if remove == True:
			total_removed += 1
			print(i)
			p = parents[i]
			for c in children[i]:
				parents[c] = p
			del children[i]
	if total_removed == 0:
		print(-1)

def io():
	given_tests = []
	total_nodes = int(sys.stdin.readline(), 10)
	nodes = [] * total_nodes
	for n in range(0, total_nodes):
		toks = sys.stdin.readline().strip().split()
		nodes.append((toks[0], toks[1]))
	remove_nodes(nodes)
io()


n = int(input())
col = [0] * (n + 1)
det = {}
for i in range(n):
	det[i + 1] = []
for i in range(n):
	(x, y) = map(int, input().split())
	if x > 0:
		det[x].append(i + 1)
	col[i + 1] = y
ans = []
for i in range(1, n + 1):
	if col[i] == 1:
		for c in det[i]:
			if col[c] == 0:
				break
		else:
			ans.append(i)
ans.sort()
if len(ans) == 0:
	print(-1)
else:
	print(*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=' ')


import collections
n = int(input())
graph = collections.defaultdict(list)
respect = {}
for i in range(1, n + 1):
	(p, res) = map(int, input().split())
	if p == -1:
		respect[i] = True
		continue
	graph[p].append(i)
	respect[i] = not res
ans = []
for i in range(1, n + 1):
	if respect[i] == False:
		n = len(graph[i])
		sums = 0
		for no in graph[i]:
			sums += int(respect[no])
		if sums == 0:
			ans.append(i)
if len(ans) == 0:
	print(-1)
else:
	print(*ans)


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)


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))


n = int(input())
A = [0 for _ in range(n + 2)]
B = [0 for _ in range(n)]
for i in range(n):
	(x, y) = map(int, input().split())
	A[i] += 1
	A[x - 1] += 1
	if y:
		B[i] += 1
		B[x - 1] += 1
ans = []
for i in range(n):
	if A[i] == B[i]:
		ans.append(i + 1)
if len(ans) > 0:
	print(*ans)
else:
	print(-1)


n = int(input())
d = {}
for i in range(1, n + 1):
	d[i] = []
start = 0
arr = [-1] * (n + 1)
k = 1
for i in range(n):
	(p, c) = map(int, input().split(' '))
	arr[k] = c
	if p == -1:
		start = k
	else:
		d[p].append(k)
		d[k].append(p)
	k += 1
ans = []
stack = [start]
visited = [False] * (n + 1)
while len(stack) != 0:
	v = stack.pop()
	if not visited[v]:
		visited[v] = True
		stack_aux = []
		cnt = 0
		count = 0
		for w in d[v]:
			if not visited[w]:
				cnt += 1
				stack_aux.append(w)
				if arr[w] == 1:
					count += 1
		if arr[v] == 1:
			if cnt == count:
				ans.append(v)
		while len(stack_aux) != 0:
			stack.append(stack_aux.pop())
if len(ans) == 0:
	print(-1)
else:
	ans = sorted(ans)
	for i in ans:
		print(i, end=' ')


from sys import setrecursionlimit
setrecursionlimit(10 ** 9)
import threading
threading.stack_size(67108864)

def main():
	n = int(input())
	u = []
	for i in range(n):
		u.append([])
	c = []
	p = []
	for i in range(n):
		(pi, ci) = map(int, input().split())
		if pi != -1:
			u[pi - 1].append(i)
			u[i].append(pi - 1)
		else:
			r = i
		c.append(ci)
		p.append(pi - 1)
	d = []
	use = [0] * n

	def dfs(v):
		ok = True
		use[v] = 1
		for i in u[v]:
			if i != p[v] and c[i] == 0:
				ok = False
				break
		if ok and c[v]:
			d.append(v + 1)
		for i in u[v]:
			if i != p[v] and use[i] == 0:
				dfs(i)
	dfs(r)
	d.sort()
	print(' '.join(map(str, d)))
	if d == []:
		print(-1)
thread = threading.Thread(target=main)
thread.start()
thread.join()


def main():
	n = int(input())
	par = {}
	chi = {i: 0 for i in range(1, n + 1)}
	fl = 0
	uvaj = {}
	for i in range(n):
		(p, c) = map(int, input().split())
		if p != -1:
			par[i + 1] = p
			chi[p] += 1 - c
		uvaj[i + 1] = 1 - c
	for i in range(1, n + 1):
		g = chi[i]
		if g == 0 and uvaj[i] == 0:
			print(i, end=' ')
			fl = 1
	if fl == 0:
		print(-1)
main()


tree = dict()
h = []
for i in range(int(input())):
	(a, b) = map(int, input().split())
	if b == 0:
		tree[a] = 0
	elif b == 1:
		if i + 1 not in tree or tree[i + 1] != 0:
			tree[i + 1] = 1
for j in tree:
	if tree[j] == 1:
		h.append(j)
if h == []:
	print(-1)
else:
	h.sort()
	print(*h)


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)


from collections import defaultdict
n1 = int(input())
a = defaultdict(list)
parent = [0] * (n1 + 1)
res = [0] * (n1 + 1)
b = []
for i in range(1, n1 + 1):
	(p, r) = map(int, input().split())
	parent[i] = p
	res[i] = r
	a[p].append(i)
for i in range(1, n1 + 1):
	if res[i] == 1:
		work = True
		for j in a[i]:
			if res[j] == 0:
				work = False
				break
		if work:
			b.append(i)
b.sort()
if len(b) != 0:
	print(*b)
else:
	print('-1')


n = int(input())
nodes = {}
child_count = [0] * (n + 1)
target_nodes = set()
for v in range(1, n + 1):
	(p, c) = map(int, input().split())
	if p != -1:
		child_count[p] += 1
	if c == 1:
		try:
			nodes[p].append(v)
		except KeyError:
			nodes[p] = [v]
		target_nodes.add(v)
ans = []
for node in list(target_nodes):
	try:
		if child_count[node] == len(nodes[node]):
			ans.append(node)
	except KeyError:
		if child_count[node] == 0:
			ans.append(node)
if ans == []:
	print(-1)
else:
	ans.sort()
	for i in ans:
		print(i, end=' ')
	print()


n = int(input())
respect = [0 for _ in range(n)]
child_respect = [0 for _ in range(n)]
root = -1
for i in range(n):
	(p, c) = map(int, input().split())
	if p == -1:
		root = i
		continue
	respect[i] = c
	if p != -1 and (not c):
		child_respect[p - 1] = 1
res = []
for i in range(n):
	if i == root:
		continue
	if respect[i] and (not child_respect[i]):
		res.append(i + 1)
if res:
	print(*res)
else:
	print(-1)


from collections import defaultdict
n = int(input())
mark = defaultdict(int)
for i in range(n):
	(x, y) = tuple(map(int, input().split()))
	c = (y + 1) % 2
	if c == 1:
		mark[x] = 1
		mark[i + 1] = 1
ans = []
for i in range(n):
	if mark[i + 1] == 0:
		ans.append(i + 1)
if len(ans) == 0:
	ans = [-1]
print(*sorted(ans))


from sys import stdin, stdout

def dfs(node):
	stack = [node]
	while stack:
		cur = stack.pop()
		notVisited[cur] = False
		condn = False if not R[cur] else True
		for i in adj[cur]:
			if notVisited[i]:
				if condn and (not R[i]):
					condn = False
				stack.append(i)
		if condn:
			res.append(cur)
N = int(stdin.readline().strip())
notVisited = [True] * (N + 1)
adj = [[] for __ in range(N + 1)]
R = [0] * (N + 1)
res = []
parent = 1
for i in range(1, N + 1):
	(u, r) = (int(num) for num in stdin.readline().split())
	if u > 0:
		adj[u].append(i)
	else:
		parent = i
	R[i] = r
dfs(parent)
if res:
	res.sort()
	print(*res)
else:
	print(-1)


v = int(input())
need_check = {}
tree = {}
kids = {}
count = 1
while count <= v:
	(parent, to_check) = 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()):
	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)))


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())
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
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)


d = {}
set_no = set({})
for i in range(int(input())):
	d[i + 1] = [int(x) for x in input().split()]
	if d[i + 1][1] == 0:
		set_no.add(i + 1)
		set_no.add(d[i + 1][0])
dif = set(set(d.keys()) - set_no)
if dif == set():
	print(-1)
else:
	for i in dif:
		print(i, end=' ')


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(n,) = I()
r = 0
g = [1] * n
p = [1] * n
c = [0] * n
for i in range(n):
	(a, b) = I()
	if a != -1:
		p[i] = a
		c[i] = b
		g[a - 1] *= b
		g[i] *= b
	else:
		r = i
		g[i] = 0
if 1 not in g:
	print(-1)
for i in range(n):
	if g[i]:
		print(i + 1, end=' ')


from collections import deque

class Node:

	def __init__(self, index, parent, respect, children=[]):
		self.index = index
		self.parent = parent
		self.respect = respect
		self.children = children

	def getinfo(self):
		return str(self.index) + ' ' + str(self.parent) + ' ' + str(self.respect)
n = int(input())
tree = [Node(i, -1, 0, []) for i in range(n)]
for i in range(n):
	(tree[i].parent, tree[i].respect) = map(int, input().split())
	if tree[i].parent != -1:
		tree[tree[i].parent - 1].children.append(i)
	else:
		root = i
result = []
queue = deque()
used = set()
queue.append(root)
used.add(root)
while len(used) != n:
	cur = queue.popleft()
	used.add(cur)
	disrespect = 0
	for c in tree[cur].children:
		queue.append(tree[c].index)
		disrespect += tree[c].respect
	if disrespect == len(tree[cur].children) and tree[cur].respect == 1:
		result.append(cur + 1)
if len(result) == 0:
	print(-1)
else:
	result.sort()
	print(' '.join(map(str, result)))


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 sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
n = int(input())
s = set()
d = {i: set() for i in range(1, n + 1)}
start = 0
for i in range(n):
	(a, b) = map(int, input().split())
	if b == 1:
		s.add(i + 1)
	if a == -1:
		start = i + 1
	else:
		d[a].add(i + 1)
z = []
stack = [start]
while stack:
	a = stack.pop()
	if a in s:
		flag = 1
		for i in d[a]:
			if i not in s:
				flag = 0
		if flag == 1:
			z.append(a)
	for i in d[a]:
		stack.append(i)
if len(z) == 0:
	print(-1)
else:
	z.sort()
	print(*z)


from collections import defaultdict

def DFS(child, parent, visited, x):
	stack = [x]
	ans = []
	while len(stack):
		temp = stack.pop()
		if temp != -1:
			visited[temp] = 1
		flag = 1
		for j in child[temp]:
			if visited[j] == 0:
				stack.append(j)
			flag = flag & parent[j][1]
		if temp != -1:
			if flag == 1 and parent[temp][1] == 1:
				ans.append(temp)
	if not len(ans):
		return [-1]
	return sorted(ans)
t = int(input())
parent = defaultdict(list)
child = defaultdict(list)
visited = [0 for i in range(t + 1)]
for i in range(t):
	(p, c) = map(int, input().split())
	child[p].append(i + 1)
	parent[i + 1].append(p)
	parent[i + 1].append(c)
x = -1
print(*DFS(child, parent, visited, x))


n = int(input())
c = [0 for i in range(0, n + 1)]
p = [0 for i in range(0, n + 1)]
childs = [[] for i in range(0, n + 1)]
for i in range(1, n + 1):
	starr = input().split()
	pi = int(starr[0])
	ci = int(starr[1])
	p[i] = pi
	if pi != -1:
		childs[pi].append(i)
	c[i] = ci
s = ''
for i in range(1, n + 1):
	if p[i] == -1:
		continue
	if c[i] == 0:
		continue
	flag = True
	for x in childs[i]:
		if c[x] == 0:
			flag = False
	if flag:
		for x in childs[i]:
			p[x] = p[i]
		if s:
			s += ' ' + str(i)
		else:
			s = str(i)
if not s:
	print(-1)
else:
	print(s)


from collections import defaultdict as DD
from bisect import bisect_left as BL
from bisect import bisect_right as BR
from itertools import combinations as IC
from itertools import permutations as IP
from random import randint as RI
import sys
MOD = pow(10, 9) + 7

def bfs(s):
	level = {s: 0}
	i = 1
	ans = []
	frontier = [s]
	while frontier:
		nex = []
		for u in frontier:
			c = 0
			for v in d[u]:
				if v not in level:
					if color[v] == 1 and color[u] == 1:
						c += 1
					level[v] = i
					nex.append(v)
			if c == len(d[u]) and color[u] == 1:
				ans.append(u)
		frontier = nex
		i += 1
	return ans

def IN(f=0):
	if f == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
n = IN(1)
d = DD(list)
color = [0 for i in range(n + 2)]
s = -1
for i in range(n):
	(u, v) = IN()
	if u == -1:
		s = i + 1
	else:
		d[u].append(i + 1)
		color[i + 1] = v
k = bfs(s)
k.sort()
if len(k) == 0:
	print(-1)
else:
	print(*k)


n = int(input())
c = [False for i in range(n)]
d = [False for i in range(n)]
for i in range(n):
	(p, b) = map(int, input().split())
	c[i] = b == 0
	if not p == -1:
		d[p - 1] |= c[i]
ans = []
for i in range(n):
	if not c[i] and (not d[i]):
		ans.append(i + 1)
if len(ans) == 0:
	print(-1)
else:
	print(*iter(ans))


from bisect import bisect_left, bisect_right, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd
from collections import deque

def data():
	return sys.stdin.readline().strip()

def mdata():
	return map(int, data().split())
n = int(data())
l = [0] * n
tree = [{} for i in range(n)]
root = -1
ans = []
for i in range(n):
	(p, c) = mdata()
	if p == -1:
		root = i
	else:
		tree[p - 1][i] = 1
		l[p - 1] += abs(c)
	l[i] += abs(c)
for i in range(n):
	if l[i] == len(tree[i]) + 1:
		ans.append(i + 1)
if len(ans) == 0:
	print(-1)
else:
	ans.sort()
	print(*ans)


from collections import defaultdict
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
n = int(input())
a = defaultdict(list)
parent = [0] * (n + 1)
res = [0] * (n + 1)
b = []
for i in range(1, n + 1):
	(p, r) = map(int, input().split())
	parent[i] = p
	res[i] = r
	a[p].append(i)
for i in range(1, n + 1):
	if res[i] == 1:
		work = True
		for j in a[i]:
			if res[j] == 0:
				work = False
				break
		if work:
			b.append(i)
b.sort()
if len(b) != 0:
	print(*b)
else:
	print('-1')


from collections import defaultdict

def main():
	n = int(input())
	bad_v = [True] * n
	for i in range(n):
		(v, r) = list(map(int, input().split(' ')))
		if r == 0 and v != -1:
			bad_v[v - 1] = False
		if v == -1:
			bad_v[i] = False
		if r == 0:
			bad_v[i] = False
	answer = [str(i + 1) for i in range(n) if bad_v[i]]
	if answer:
		print(' '.join(answer))
	else:
		print(-1)
main()


no = int(input())
arr = [0] * (no + 1)
ans = []
tree = [list() for i in range(no + 1)]
for i in range(1, no + 1):
	arr[i] = list(map(lambda x: int(x), input().split()))

def isAllitsChild(x, arr):
	for i in tree[x]:
		if arr[i][1] == 0:
			return False
	return True

def transferChild(x):
	for i in tree[x]:
		arr[i][0] = arr[x][0]
for i in range(1, no + 1):
	if arr[i][0] != -1:
		tree[arr[i][0]].append(i)
for i in range(1, no + 1):
	if isAllitsChild(i, arr) and arr[i][1] == 1:
		transferChild(i)
		arr[i][0] = -2
		ans.append(i)
if len(ans) != 0:
	for i in ans:
		print(i, end=' ')
else:
	print(-1)


import sys
from collections import deque
input = sys.stdin.readline
print = sys.stdout.write

def solve():
	n = int(input())
	c = [0 for _ in range(n)]
	edges = dict()
	for i in range(n):
		edges[i] = list()
	root = 0
	for i in range(n):
		pc = input().split()
		p = int(pc[0])
		c_input = int(pc[1])
		c[i] = c_input
		if p == -1:
			root = i
		else:
			edges[i].append(p - 1)
			edges[p - 1].append(i)
	result = list()
	visited = [False for _ in range(n)]
	q = deque()
	q.append(root)
	while q:
		cur = q.popleft()
		visited[cur] = True
		all_disrepect = True
		for neighbor in edges[cur]:
			if not visited[neighbor]:
				q.append(neighbor)
				visited[neighbor] = True
				if not c[neighbor]:
					all_disrepect = False
		if all_disrepect and c[cur]:
			result.append(cur)
	result = sorted(result)
	for i in range(len(result)):
		print(f'{result[i] + 1} ')
	if len(result) == 0:
		print('-1')
solve()


import bisect
n = int(input())
tree = {}
respect = []
parentof = {}
root = -1
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	respect.append(c)
	if p == -1:
		root = i
	else:
		if p in tree.keys():
			tree[p].append(i)
		else:
			tree[p] = [i]
		parentof[i] = p
final = []
for i in range(0, len(respect)):
	if respect[i] == 1 and i + 1 != root:
		if i + 1 in tree:
			for j in range(0, len(tree[i + 1])):
				if respect[tree[i + 1][j] - 1] == 0:
					break
			else:
				final.append(i + 1)
		else:
			final.append(i + 1)
final.sort()
if len(final) == 0:
	print(-1)
else:
	ans = []
	j = 0
	while j < len(final):
		x = final[j]
		p = parentof[x]
		for k in range(0, len(tree[p])):
			if respect[tree[p][k] - 1] == 1:
				break
		else:
			bisect.insort_left(final, p)
		ans.append(final[j])
		j += 1
	for j in range(0, len(ans)):
		print(ans[j], end=' ')
	print()


from collections import defaultdict
n = int(input())
v = [0]
g = defaultdict(set)
root = None
is_candidates = [True] * (n + 1)
for i in range(1, n + 1):
	(parent, respect) = map(int, input().split())
	v.append([parent, respect])
	if respect == 0 and parent >= 0:
		is_candidates[parent] = False
bad_sons = []
for i in range(1, n + 1):
	if is_candidates[i] and v[i][1] == 1:
		bad_sons.append(i)
if len(bad_sons) == 0:
	print(-1)
else:
	print(' '.join(map(str, sorted(bad_sons))))


def chk(node):
	global g, p, c
	temp = 0
	g[node].remove(p[node])
	for i in g[node]:
		temp += c[i]
	if temp == len(g[node]):
		g[node].append(p[node])
		return 1
	g[node].append(p[node])
	return 0

def del_node(node):
	global g, c, p
	g[node].remove(p[node])
	for i in g[node]:
		g[i].remove(node)
		g[i].append(p[node])
		p[i] = p[node]
	g[p[node]].remove(node)
	g[p[node]].extend(g[node])
v = int(input())
c = {}
p = {}
g = {}
ans = []
for i in range(1, v + 1):
	g[i] = []
for i in range(1, v + 1):
	(x, y) = map(int, input().split())
	p[i] = x
	c[i] = y
	if x != -1:
		g[i].append(x)
		g[x].append(i)
for i in range(1, v + 1):
	if p[i] != -1 and c[i] and chk(i):
		ans.append(i)
if len(ans) == 0:
	print(-1)
else:
	for i in ans:
		print(i, end=' ')


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(''))
tree = [0 for i in range(n + 1)]
qc = []
for i in range(n):
	(a, b) = [int(x) for x in input('').split(' ')]
	if b == 0:
		tree[i + 1] = 1
		if a > 0:
			tree[a] = 1
for i in range(1, n + 1):
	if tree[i] == 0:
		qc.append(i)
if qc:
	for i in qc:
		print(i, end=' ')
else:
	print(-1)


import sys
input = sys.stdin.readline
n = int(input())
parent_children = [[] for _ in range(n + 1)]
respects_parent = [None] * (n + 1)
for child in range(1, n + 1):
	(parent, respect) = map(int, input().split())
	if parent == -1:
		root = child
		continue
	parent_children[parent].append(child)
	respects_parent[child] = respect
to_cut = [0] * (n + 1)
from collections import deque
visited = [0] * (n + 1)
stack = deque([root])
visited[root] = 1
while stack:
	node = stack.pop()
	r = respects_parent[node]
	for child in parent_children[node]:
		r = r and respects_parent[child]
		if not visited[child]:
			stack.append(child)
			visited[child] = 1
	to_cut[node] = r
res = []
for node in range(1, n + 1):
	if to_cut[node] and node != root:
		res.append(node)
if len(res):
	print(*sorted(res))
else:
	print(-1)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False

def ok():
	print('OK')
N = 10 ** 9 + 7
n = I()
val = [0] * n
ans = []
a = [[] for i in range(n)]
for i in range(n):
	(p, c) = R()
	if p != -1:
		a[p - 1] += (i,)
		val[i] = c
for i in range(n):
	if val[i]:
		flg = True
		for el in a[i]:
			if not val[el]:
				flg = False
				break
		if flg:
			ans += (i + 1,)
print(*(ans if ans else [-1]))


import sys
readline = sys.stdin.readline

def i1():
	return int(readline())

def nl():
	return [int(s) for s in readline().split()]

def nn(n):
	return [int(readline()) for i in range(n)]

def nnp(n, x):
	return [int(readline()) + x for i in range(n)]

def nmp(n, x):
	return (int(readline()) + x for i in range(n))

def nlp(x):
	return [int(s) + x for s in readline().split()]

def nll(n):
	return [[int(s) for s in readline().split()] for i in range(n)]

def mll(n):
	return ([int(s) for s in readline().split()] for i in range(n))

def s1():
	return readline().rstrip()

def sl():
	return [s for s in readline().split()]

def sn(n):
	return [readline().rstrip() for i in range(n)]

def sm(n):
	return (readline().rstrip() for i in range(n))

def redir(s):
	global readline
	import os
	fn = sys.argv[0] + f'/../in-{s}.txt'
	readline = open(fn).readline if os.path.exists(fn) else readline
redir('c')
n = i1()
flag = [False] * (n + 1)
cand = []
children = [[] for i in range(n + 2)]
for i in range(1, n + 1):
	(p, c) = readline().split()
	if c == '1':
		flag[i] = True
		cand.append(i)
	children[int(p)].append(i)
lst = []
for c in cand:
	if all((flag[i] for i in children[c])):
		lst.append(c)
print(' '.join((str(i) for i in lst)) if lst else -1)


import os
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
dels = []
cof = [0] * n
G = [list() for _ in range(n)]
for v in range(n):
	(p, c) = map(int, input().split())
	if p == -1:
		root = v
	else:
		p = p - 1
		G[p].append(v)
	cof[v] = c
stack = [root]
while stack:
	u = stack.pop()
	d = False
	if cof[u] == 1:
		d = True
	for v in G[u]:
		if cof[v] == 0:
			d = False
		stack.append(v)
	if d:
		dels.append(u + 1)
if dels:
	print(*sorted(dels))
else:
	print(-1)


import sys

def input():
	return sys.stdin.buffer.readline().strip()
n = int(input())
notremove = [0] * (n + 1)
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	if c == 0:
		notremove[i] = 1
		if p > 0:
			notremove[p] = 1
check = 0
for i in range(1, n + 1):
	if notremove[i] == 0:
		check = 1
		print(i, end=' ')
if check == 0:
	print(-1)


n = int(input())
parent = [-1] * n
isresp = [1] * n
for i in range(n):
	(p, r) = 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)))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = set()
l = []
for i in range(n):
	(m, n) = map(int, input().split())
	if n == 1:
		l.append(i + 1)
	else:
		a.add(m)
l.sort()
t = 0
for j in l:
	if j not in a:
		t = 1
		print(j, end=' ')
if t == 0:
	print(-1)


import sys, math, heapq
from collections import deque

def get_int():
	return list(map(int, sys.stdin.readline().strip().split()))

class Tree_node:

	def __init__(self, name):
		self.name = name
		self.parent = None
		self.respect = -1
		self.child = []

class Tree:

	def __init__(self, N):
		self.v_count = N
		self.vertices = [Tree_node(n) if n > 0 else None for n in range(N + 1)]
		self.root = -1

	def add_enfo(self, v, p, r):
		v_obj = self.vertices[v]
		if p == -1:
			self.root = v
			v_obj.respect = r
		else:
			p_obj = self.vertices[p]
			p_obj.child.append(v)
			v_obj.parent = p
			v_obj.respect = r

def bfs(T_obj, root):
	q = deque([root])
	d_nodes = []
	while len(q) > 0:
		v = T_obj.vertices[q.popleft()]
		c_r = False
		if v.respect == 1:
			c_r = True
		for c in v.child:
			q.append(c)
			if T_obj.vertices[c].respect == 0:
				c_r = False
		if c_r == True:
			heapq.heappush(d_nodes, v.name)
	return d_nodes
n = int(input())
my_3 = Tree(n)
for v in range(1, n + 1):
	(p, c) = get_int()
	my_3.add_enfo(v, p, c)
dlt = bfs(my_3, my_3.root)
if len(dlt) > 0:
	while len(dlt) > 0:
		print(heapq.heappop(dlt), end=' ')
	print()
else:
	print(-1)


import sys
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

def bfs(root, c, r):
	(q, d) = ([], [])
	q.append(root)
	while len(q) != 0:
		x = q.pop(0)
		flag = False
		if r[x] == 1:
			flag = True
		for child in c[x]:
			if r[child] == 0:
				flag = False
			q.append(child)
		if flag:
			d.append(x)
	return d

def main():
	n = int(input())
	c = [[] for _ in range(n + 1)]
	r = [0] * (n + 1)
	root = 0
	for i in range(1, n + 1):
		(x, y) = get_ints()
		if x != -1:
			c[x].append(i)
		else:
			root = i
		r[i] = y
	d = bfs(root, c, r)
	if len(d) == 0:
		print('-1')
	else:
		d.sort()
		print_array(d)
main()


from collections import defaultdict
from sys import stdin
input = stdin.readline
n = int(input())
parent = [None] * (n + 1)
children = defaultdict(list)
respectful = [None] * (n + 1)
for i in range(1, n + 1):
	(pi, ci) = map(int, input().split())
	parent[i] = pi
	children[pi].append(i)
	respectful[i] = ci
root = children[-1][0]
stk = [root]
to_delete = []
checked = {}
while len(stk) != 0:
	d = stk.pop()
	for ch in children[d]:
		if checked.get(ch, False):
			continue
		if respectful[ch] == 1:
			to_remove = True
			for child in children[ch]:
				if respectful[child] == 0:
					to_remove = False
					break
			if to_remove:
				to_delete.append(ch)
				chds = children[d]
				chds.remove(ch)
				children[d] = chds
				for child in children[ch]:
					children[d].append(child)
					parent[child] = d
				stk.append(d)
			else:
				stk.append(ch)
				checked[ch] = True
		else:
			stk.append(ch)
			checked[ch] = True
to_delete.sort()
if len(to_delete) > 0:
	print(*to_delete)
else:
	print(-1)


n = int(input())
p = [None] * n
c = [None] * n
r = []
for i in range(0, n):
	c[i] = []
for i in range(0, n):
	(pa, l) = list(map(int, input().split()))
	if pa != -1:
		c[pa - 1].append(i)
	p[i] = [pa - 1, l]
for i in range(0, n):
	if p[i][0] < 0 or p[i][1] == 0:
		continue
	count = 0
	for j in range(0, len(c[i])):
		if p[c[i][j]][1] == 1:
			count += 1
	if count == len(c[i]):
		r.append(i + 1)
if len(r) == 0:
	print(-1)
else:
	for i in range(0, len(r)):
		print(r[i], end=' ')


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(sys.stdin.readline())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def myceil(x, y):
	return (x + y - 1) // y

def Solution(hm, adjList, n):
	f = 1
	for i in range(1, n + 1):
		if hm[i][1] == -1:
			continue
		if hm[i][1] == 1:
			flg = 1
			for child in adjList[i]:
				if hm[child][1] == 0:
					flg = 0
					break
			if flg:
				f = 0
				print(i, end=' ')
	if f:
		print(-1)
	else:
		print()

def main():
	n = get_int()
	hm = {}
	adjList = [[] for _ in range(n + 1)]
	for i in range(n):
		(u, v) = get_ints_in_variables()
		hm[i + 1] = [u, v]
		if u != -1:
			adjList[u].append(i + 1)
	Solution(hm, adjList, n)
main()


GI = lambda : int(input())
GIS = lambda : map(int, input().split())
LGIS = lambda : list(GIS())

def main():
	n = GI()
	respectful = [0] * (n + 1)
	respected = [0] * (n + 1)
	for (i, (p, c)) in enumerate((GIS() for i in range(n)), 1):
		if c == 0:
			respectful[i] = 1
			if p != -1:
				respected[p] = 1
	to_remove = [i for i in range(1, n + 1) if respected[i] == respectful[i] == 0]
	if to_remove:
		print(' '.join(map(str, to_remove)))
	else:
		print(-1)
main()


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)


n = int(input())
p = n * [-1]
c = n * [1]
for i in range(n):
	(p[i], c[i]) = map(int, input().split())
	p[i] -= 1
cc = c[:]
for i in range(n):
	if p[i] >= 0:
		cc[p[i]] = cc[p[i]] * c[i]
answer = ''
for (i, x) in enumerate(cc):
	if x:
		answer += f'{i + 1} '
if answer:
	print(answer)
else:
	print(-1)


n = int(input())
l = []
g = [[] for _ in range(n)]
r = 0
for i in range(n):
	(p, b) = map(int, input().split())
	if p == -1:
		r = i
	else:
		g[p - 1].append(i)
	l.append(1 - b)
ans = []

def travel(r):
	st = [(r, r)]
	while st:
		u = st.pop()
		for v in g[u[0]]:
			st.append((v, u[0]))
		l[u[1]] += l[u[0]]
	ans = []
	for i in range(n):
		if l[i] == 0:
			ans.append(i)
	return ans
ans = travel(r)
if len(ans) == 0:
	print(-1)
else:
	print(' '.join((str(i + 1) for i in ans)))


n = int(input())
children = {}
tree = [None]
c = [None]
all = [True for _ in range(n + 1)]
root = None
for i in range(1, n + 1):
	(p, ci) = [int(x) for x in input().split(' ')]
	tree += [p]
	c += [ci]
	children.setdefault(p, []).append(i)
	if p == -1:
		root = i
	if ci == 0:
		all[p] = False
deleted = []
for i in range(1, n + 1):
	if c[i] == 1:
		rm = True
		if i in children:
			for child in children[i]:
				if c[child] == 0:
					rm = False
					break
		if rm:
			if i in children:
				for child in children[i]:
					tree[child] = tree[i]
			if tree[i] in children and i in children[tree[i]]:
				children[tree[i]].remove(i)
			deleted.append(str(i))
print('-1' if len(deleted) == 0 else ' '.join(deleted))


n = int(input())
node___parent = [-1 for _ in range(n)]
node___disrespectfull = [-1 for _ in range(n)]
node___children = [set() for _ in range(n)]
for i in range(n):
	(p, r) = (int(x) for x in input().split())
	node___parent[i] = p - 1
	node___disrespectfull[i] = r
	if p != -1:
		node___children[p - 1].add(i)
node___to_delete = [False for _ in range(n)]
to_delete_list = []
for i in range(n):
	if node___parent[i] < 0:
		continue
	if node___disrespectfull[i] == 0:
		continue
	node___to_delete[i] = True
	for child in node___children[i]:
		if node___disrespectfull[child] == 0:
			node___to_delete[i] = False
	if node___to_delete[i]:
		to_delete_list.append(i + 1)
if to_delete_list:
	print(*to_delete_list)
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())
tree = {}
is_bad = {}
bad = set([])
for i in range(n):
	(p, c) = map(int, input().split())
	p -= 1
	if p not in tree:
		tree[p] = set([i])
	else:
		tree[p].add(i)
	if c == 1:
		is_bad[i] = 1
		bad.add(i)
	else:
		is_bad[i] = 0
to_remove = []
for v in bad:
	flag = True
	if v not in tree:
		if is_bad[v] == 1:
			to_remove.append(v)
		continue
	for child in tree[v]:
		if is_bad[child] == 0:
			flag = False
			break
	if flag:
		to_remove.append(v)
to_remove = sorted(to_remove)
if len(to_remove) == 0:
	print(-1)
else:
	for v in to_remove:
		print(v + 1, end=' ')


def main():
	n = int(input())
	g = [[] for _ in range(n + 1)]
	c = [False]
	root = -1
	for i in range(1, n + 1):
		(parent, ci) = map(int, input().split())
		if parent == -1:
			root = i
		else:
			g[parent].append(i)
		c.append(bool(ci))
	cand = []
	for node in range(1, n + 1):
		if c[node] and all((c[x] for x in g[node])):
			cand.append(node)
	print(' '.join((str(i) for i in sorted(cand))) if len(cand) > 0 else '-1')
main()


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
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])
ans = bfs(graph, parent, n)
ans.sort()
if len(ans) > 0:
	print(*ans)
else:
	print(-1)


def solve(edges, respect, root):
	delete_order = []
	stack = [root]
	visited = set()
	while stack:
		curr = stack.pop()
		if curr not in visited:
			visited.add(curr)
			count = 0
			for kid in edges[curr]:
				if kid not in visited:
					if respect[kid - 1] == 1:
						count += 1
				stack.append(kid)
			if count == len(edges[curr]) - 1 and respect[curr - 1] == 1:
				delete_order.append(curr)
	if not delete_order:
		print(-1)
		return
	delete_order.sort()
	for i in delete_order:
		print(i, end=' ')

def main():
	n = int(input())
	edges = {}
	for i in range(1, n + 1):
		edges[i] = []
	respect = [0] * n
	for i in range(1, n + 1):
		(p, c) = map(int, input().split())
		if p != -1:
			edges[p].append(i)
			edges[i].append(p)
		respect[i - 1] = c
		if p == -1:
			root = i
	solve(edges, respect, root)
main()


n = int(input())
a = []
res = []
parent = [[] for i in range(n + 1)]
for i in range(n):
	(n1, n2) = map(int, input().split())
	n1 -= 1
	if n1 == -2:
		n1 += 1
	a.append([n1, n2])
	parent[n1].append(i)
used = []
flagg = True
for i in range(len(a)):
	if a[i][1] == 1:
		flag = True
		for l in parent[i]:
			if a[l][1] == 0:
				flag = False
		if flag:
			res.append(i + 1)
res.sort()
if len(res) == 0:
	print(-1)
else:
	for i in res:
		print(i)


class TreeNode:

	def __init__(self, val, parent, respect):
		self.val = val
		self.parent = parent
		self.respect = respect
		self.childs = set()

	def add_child(self, vertex):
		self.childs.add(vertex)

	def get_childs(self):
		return self.childs

	def to_delete(self, tree):
		if self.respect == 0:
			return False
		for v in self.get_childs():
			node = tree[v - 1]
			if node.respect == 0:
				return False
		return True
N = int(input())
tree = []
for i in range(1, N + 1):
	(p, r) = map(int, input().split())
	node = TreeNode(i, p, r)
	tree.append(node)
for i in range(1, N + 1):
	p = tree[i - 1].parent
	if p != -1:
		tree[p - 1].add_child(i)
to_delete = []
for i in range(1, N + 1):
	if tree[i - 1].to_delete(tree):
		to_delete.append(i)
if to_delete:
	print(*sorted(to_delete))
else:
	print(-1)


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)


import math
import sys
from collections import defaultdict
from collections import Counter
from collections import deque
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]
INF = 10 ** 18
MOD = 10 ** 9 + 7
first = 1
G = defaultdict(list)

def addEdge(a, b):
	G[a].append(b)
	G[b].append(a)

def BFSUtil(queue, vis, parent):
	while queue:
		s = queue.popleft()
		for child in G.get(s, []):
			if vis[child] == False:
				queue.append(child)
				parent[child] = s
				vis[child] = True

def findparent(node, N):
	vis = [False for i in range(N + 1)]
	parent = {}
	queue = deque()
	parent[node] = -1
	queue.append(node)
	vis[node] = True
	BFSUtil(queue, vis, parent)
	return parent
root = 1
N = int(input())
mark = {}
for i in range(N):
	(u, v) = ilele()
	if u == -1:
		root = i + 1
	else:
		addEdge(i + 1, u)
	mark[i + 1] = v
P = findparent(root, N)
Ans = set()
for (i, j) in G.items():
	x = P.get(i, -1)
	if mark.get(i, -1) == 1:
		f = 0
		for k in j:
			if k != x and mark.get(k, -1) == 0:
				f = 1
				break
		if f == 0:
			Ans.add(i)
if len(Ans):
	print(*sorted(list(Ans)))
else:
	print(-1)


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
import math
import random
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN():
	return sys.stdin.readline().strip().split()

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5
n = int(input())
par = defaultdict(int)
res = defaultdict(int)
for i in range(1, n + 1):
	res[i] = 1
l = []
for i in range(1, n + 1):
	(p, c) = numIN()
	if p == -1:
		root = i
		continue
	if c == 1:
		l.append(i)
	par[i] = p
	res[p] &= c
f = 0
for i in l:
	if res[i]:
		f = 1
		print(i, end=' ')
		res[par[i]] &= res[i]
if not f:
	print(-1)


from collections import defaultdict
record = defaultdict(list)
parent = defaultdict(list)
n = int(input())
for i in range(n):
	(p, c) = map(int, input().split())
	record[i + 1].extend([p, c])
	parent[p].append(i + 1)
flag1 = 1
for i in range(1, n + 1):
	if record[i][1] == 1:
		flag = 1
		for j in parent[i]:
			if record[j][1] == 0:
				flag = 0
				break
		if flag == 1:
			flag1 = 0
			for j in parent[i]:
				record[j][0] = record[i][0]
			print(i, end=' ')
if flag1 == 1:
	print(-1)


import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B

@atexit.register
def write():
	sys.__stdout__.write(_O_B.getvalue())
n = int(input())
tree = dict()
for i in range(n):
	tree[i + 1] = [[], [], -1, -1]
for i in range(n):
	(p, c) = map(int, input().split())
	if p != -1:
		tree[p][c].append(i + 1)
		tree[i + 1][2] = p
		tree[i + 1][3] = c
delete = []
for i in tree:
	if tree[i][2] != -1 and tree[i][3] and (len(tree[i][0]) == 0):
		delete.append(i)
if delete:
	for i in delete:
		print(i, end=' ')
	print()
else:
	print(-1)


n = int(input())
ps = [0] * (n + 1)
cs = [0] * (n + 1)
adj = [[] for _ in range(n + 1)]
root = None
for i in range(1, n + 1):
	(ps[i], cs[i]) = map(int, input().split())
	if ps[i] != -1:
		adj[i].append(ps[i])
		adj[ps[i]].append(i)
	else:
		root = i
answer = []
bfs = [root]
for node in bfs:
	if cs[node]:
		take = True
		for children in adj[node]:
			if children != ps[node]:
				if cs[children] == 0:
					take = False
		if take:
			answer.append(node)
	for children in adj[node]:
		if children != ps[node]:
			bfs.append(children)
if answer:
	print(' '.join(list(map(str, sorted(answer)))))
else:
	print(-1)


import collections
import heapq
import bisect
import math
import time

class Solution2:

	def solve(self, A1, A2):
		pass

def gcd(a, b):
	if not b:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return b * a // gcd(b, a)

class Solution:

	def solve(self, pairs):
		out = []
		graph = collections.defaultdict(list)
		respect = [False] * len(pairs)
		deleted = [False] * len(pairs)
		for (i, (j, c)) in enumerate(pairs):
			graph[j - 1].append(i)
			respect[i] = not bool(c)

		def dfs(node):
			if respect[node]:
				return False
			if all((deleted[child] or not respect[child] for child in graph[node])):
				return True
			return False
		for i in range(len(pairs)):
			if dfs(i):
				out.append(i)
				deleted[i] = True
		return [o + 1 for o in out] if out else [-1]
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
	N = input()
	a = []
	for _ in range(int(N)):
		a.append([int(c) for c in input().split()])
	out = sol.solve(a)
	print(' '.join([str(o) for o in out]))


n = int(input())
disrespecful = set(range(1, n + 1))
for i in range(1, n + 1):
	(parent, does_respect) = map(int, input().split())
	if not does_respect:
		disrespecful.discard(parent)
		disrespecful.discard(i)
if len(disrespecful):
	print(' '.join(map(str, sorted(disrespecful))))
else:
	print(-1)


n = int(input())
stat = []
parent = [0 for i in range(n)]
for i in range(n):
	(a, b) = list(map(int, input().split()))
	stat.append(b)
	parent[i] = a
ans = stat[:]
for i in range(n):
	if parent[i] != -1 and (not stat[i]):
		ans[parent[i] - 1] = 0
if list(set(ans)) == [0]:
	print(-1)
else:
	fin = []
	for i in range(len(ans)):
		if ans[i] == 1:
			fin.append(str(i + 1))
	print(' '.join(fin))


def f(graf):
	for i in graf:
		if i > 0:
			return False
	return True
n = int(input())
graf = [[]]
for i in range(n):
	graf.append([])
maspred = [0]
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	maspred.append(p)
	if c == 0:
		c = 1
	else:
		c = -1
	if p == -1:
		continue
	graf[p].append(i * c)
	graf[i].append(p * c)
ch = 0
for i in range(1, len(graf)):
	if f(graf[i]) and maspred[i] != -1:
		print(i)
		ch += 1
if ch == 0:
	print(-1)


def bfs(a, d, di):
	(queue, res) = ([a], [])
	while queue:
		x = queue.pop(0)
		k = 1
		for (i, y) in enumerate(d[x]):
			if di[y] == 0:
				k = 0
			queue.append(y)
		if di[x] == 1 and k == 1:
			res.append(x)
	if res == []:
		print(-1)
	else:
		res.sort()
		print(*res)
n = int(input())
from sys import stdin
(d, di, a) = ({i: [] for i in range(1, n + 1)}, {}, 1)
for i in range(1, n + 1):
	(p, c) = map(int, stdin.readline().split())
	if p > -1:
		d[p].append(i)
	else:
		a = i
	di[i] = c
bfs(a, d, di)


n = int(input())
cs = [0] * (n + 1)
g = [[] for _ in range(n + 1)]
root = -1
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	if p == -1:
		root = i
	else:
		g[p].append(i)
	cs[i] = c
ans = []
for i in range(1, n + 1):
	if cs[i] == 1 and (not g[i] or sum((cs[x] for x in g[i])) == len(g[i])):
		ans.append(i)
if not ans:
	print(-1)
else:
	print(*ans)


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)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
kk = lambda : map(int, input().split())
k2 = lambda : map(lambda x: int(x) - 1, input().split())
ll = lambda : list(kk())
n = int(input())
if n < 3:
	print(-1)
	exit()
pos = [True] * n
for node in range(n):
	(p, c) = kk()
	if c == 0:
		pos[node] = pos[p - 1 if p != -1 else node] = False
tbp = []
for node in range(n):
	if pos[node]:
		tbp.append(node + 1)
print(-1) if len(tbp) == 0 else print(*tbp)


import sys
from collections import defaultdict as dd
import heapq
mod = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 6)

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())

def dfs(u, par):
	f = 1 & s[u]
	for i in t[u]:
		if s[i] == 0:
			f = 0
	if f:
		ans.append(u)
	for i in t[u]:
		if i != par:
			dfs(i, u)

def bfs(start):
	bb = [start]
	vis = [0 for i in range(n + 1)]
	vis[start] = 1
	while bb:
		k = bb.pop(0)
		f = 1 & s[k]
		for i in t[k]:
			if vis[i] == 0:
				vis[i] = 1
				bb.append(i)
				f = f & s[i]
		if f == 1:
			ans.append(k)
ans = []
n = ri(1)
t = dd(list)
s = [-1 for i in range(2 * n)]
start = -1
for i in range(n):
	(p, c) = ri()
	if p == -1:
		start = i + 1
	else:
		t[p].append(i + 1)
	s[i + 1] = c
bfs(start)
if len(ans) == 0:
	print(-1)
else:
	ans.sort()
	print(*ans)


N = int(input())
boolean = {}
children = {}
for i in range(1, N + 1):
	children[i] = []
for i in range(1, N + 1):
	(x, y) = map(int, input().split())
	try:
		children[x].append(i)
	except:
		pass
	boolean[i] = y
delete = []
for curnode in range(1, N + 1):
	if boolean[curnode] == 1:
		for child in children[curnode]:
			if boolean[child] == 0:
				break
		else:
			delete.append(curnode)
delete.sort()
if len(delete) == 0:
	print(-1)
else:
	for i in delete:
		print(i, end=' ')
	print()


from heapq import *
n = int(input())
pred = [[] for i in range(n + 1)]
g = [[] for i in range(n + 1)]
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	pred[i] = [p, c]
	if p != -1:
		g[p].append([i, c])
heap = []
for i in range(1, n + 1):
	if pred[i][1] == 1:
		ok = True
		for x in g[i]:
			if x[1] == 0:
				ok = False
				break
		if ok:
			heappush(heap, i)
res = []
while heap:
	v = heappop(heap)
	res.append(v)
if not res:
	print(-1)
else:
	print(' '.join(map(str, res)))


n = int(input())
tree = [{'a': set(), 'p': 0, 'c': 0} for i in range(n + 1)]
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	a = tree[i]['a']
	tree[i] = {'a': a, 'p': p, 'c': c}
	if p != -1:
		tree[p]['a'].add(i)
can_delete = [True] * (n + 1)
can_delete[0] = False
for i in range(1, n + 1):
	if not tree[i]['c']:
		can_delete[i] = False
		if tree[i]['p'] != -1:
			can_delete[tree[i]['p']] = False
res = [i for (i, x) in enumerate(can_delete) if x]
if len(res):
	print(*res)
else:
	print(-1)


from sys import stdin, stdout, setrecursionlimit
import heapq
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007

def getAns(d, root):
	queue = deque([[root, 0]])
	res = []
	while len(queue):
		(cur, c) = queue.popleft()
		flag = True
		for (a, b) in d.get(cur, []):
			if b == 0:
				flag = False
			queue.append([a, b])
		if flag and c:
			res.append(cur)
	return res
n = ii1()
d = {}
for i in range(n):
	(p, c) = iia()
	if p == -1:
		root = i + 1
	else:
		d.setdefault(p, []).append([i + 1, c])
res = getAns(d, root)
if len(res):
	print(*sorted(res))
else:
	print(-1)


n = int(input())
degs = [0] * (n + 1)
colors = [0] * (n + 1)
dl = [0] * (n + 1)
for i in range(n):
	(v, x) = map(int, input().split())
	v = max(v, 0)
	colors[i + 1] = x
	degs[v] += 1
	if x == 1:
		dl[v] += 1
res = []
for i in range(1, n + 1):
	if colors[i] == 1 and degs[i] == dl[i]:
		res.append(i)
if res != []:
	print(*res)
else:
	print(-1)


import sys

class CQueen:

	def solve(self):
		n = int(input())
		p = [0] * n
		c = [0] * n
		exists_respect = [False] * n
		for i in range(n):
			(p[i], c[i]) = [int(_) for _ in input().split()]
			if p[i] != -1:
				exists_respect[p[i] - 1] |= bool(1 - c[i])
		ans = []
		for i in range(n):
			if not c[i] or exists_respect[i]:
				continue
			ans += [i + 1]
		if ans:
			print(*ans)
		else:
			print(-1)
solver = CQueen()
input = sys.stdin.readline
solver.solve()


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())
tree = [[] for i in range(n)]
pl = list()
cl = list()
cdoom = [True] * n
for i in range(n):
	(p, c) = map(int, input().split())
	pl.append(p - 1)
	cl.append(c)
	if p != -1:
		tree[p - 1].append(i)
		if c == 0:
			cdoom[p - 1] = False
ans = []
for i in range(n):
	if cl[i] == 0:
		continue
	if cl[i] == 1 and cdoom[i] == True:
		ans.append(str(i + 1))
if len(ans) == 0:
	print(-1)
else:
	print(' '.join(ans))


class CodeforcesTask1143CSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.vertices = []

	def read_input(self):
		self.n = int(input())
		for x in range(self.n):
			self.vertices.append([int(y) for y in input().split(' ')])

	def process_task(self):
		respected = {}
		for x in range(self.n):
			if not self.vertices[x][1]:
				respected[x + 1] = True
				respected[self.vertices[x][0]] = True
		result = []
		for x in range(self.n):
			if x + 1 not in respected:
				result.append(x + 1)
		self.result = ' '.join([str(x) for x in result]) if result else '-1'

	def get_result(self):
		return self.result
Solution = CodeforcesTask1143CSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


from collections import defaultdict as dfd
n = int(input())
respect = [0] * n
d = dfd(list)
for i in range(n):
	(a, b) = map(int, input().split())
	if a != -1:
		d[a - 1].append(i)
		respect[i] = b
arr = list(d.items())
arr.sort()
brr = dfd(int)
tried = dfd(int)
for (i, j) in arr:
	if respect[i] == 1:
		cnt = 0
		for k in j:
			if respect[k] == 1:
				cnt += 1
		if cnt == len(j):
			brr[i] = 1
		tried[i] = 1
for i in range(n):
	if respect[i] == 1 and brr[i] == 0 and (tried[i] == 0):
		brr[i] = 1
ans = []
if brr:
	for (i, j) in brr.items():
		if j == 1:
			ans.append(i + 1)
	if ans:
		ans.sort()
		print(*ans)
	else:
		print(-1)
else:
	print(-1)


from sys import *
V = int(stdin.readline())
Tree = []
for v in range(V):
	Tree += [[[], None, True]]
for v in range(V):
	(p, c) = map(int, stdin.readline().split())
	if p == -1:
		Tree[v][2] = False
	if p != -1:
		if c == 0:
			Tree[p - 1][2] = False
			Tree[v][2] = False
			Tree[v][1] = p - 1
			Tree[p - 1][0] += [v]
		else:
			Tree[p - 1][0] += [v]
			Tree[v][1] = p - 1
from heapq import *
curCan = []
for (i, t) in enumerate(Tree):
	if t[2]:
		heappush(curCan, i)
if len(curCan) == 0:
	print(-1)
else:
	while curCan != []:
		i = heappop(curCan)
		print(i + 1, end=' ')


n = int(input())
(a, b) = ({}, {})
for i in range(n):
	c = list(map(int, input().split()))
	a[i + 1] = c
	b[i + 1] = 0
for i in a:
	d = a[i]
	if d[1] == 0:
		b[i] = float('-inf')
		b[d[0]] = float('-inf')
	else:
		b[i] += 1
		b[d[0]] += 1
l = []
for i in b:
	if b[i] > 0:
		l.append(i)
if l == []:
	print(-1)
else:
	l.sort()
	print(*l)


def isvalid(l):
	global obeys
	for i in l:
		if obeys[i] == 0:
			return 0
	return 1
n = int(input())
arrofp = [0 for i in range(n + 1)]
obeys = [0 for i in range(n + 1)]
isparentof = [[] for i in range(n + 1)]
for i in range(1, n + 1):
	l = input().split()
	arrofp[i] = int(l[0])
	if arrofp[i] == -1:
		continue
	isparentof[arrofp[i]].append(i)
	obeys[i] = int(l[1])
lfi = []
for i in range(1, n + 1):
	if obeys[i] == 1:
		if isvalid(isparentof[i]):
			lfi.append(i)
if lfi == []:
	print(-1)
else:
	for i in lfi:
		print(i, end=' ')
	print()


from collections import defaultdict
d = defaultdict(list)
n = int(input())
karma = 0
care = []
for i in range(n):
	(p, c) = map(int, input().split())
	p -= 1
	if p == -1:
		continue
	d[p].append(i)
	care.append(c)
for i in range(n):
	f = 1
	for j in d[i]:
		if care[j] == 0:
			f = 0
	if f:
		if care[i] == 1:
			karma = 1
			print(i + 1, end=' ')
if karma == 0:
	print(-1)
exit()


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]

def dostuff(root):
	global respect, c
	respect[root] = c[root]
	for i in child[root]:
		respect[root] = max(respect[root], c[i])
		dostuff(i)
n = val()
respect = set([*range(1, n + 1)])
child = defaultdict(list)
c = [0]
for i in range(1, n + 1):
	(a, b) = li()
	if not b:
		if i in respect:
			respect.remove(i)
		if a in respect:
			respect.remove(a)
if len(respect):
	print(*sorted(respect))
else:
	print(-1)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def exact(num):
	if abs(num - round(num)) <= 10 ** (-9):
		return round(num)
	return num

def solve(n, lst):
	parent = {}
	respect = {}
	numberChild = [0] * n
	for i in lst:
		if i[0] != -1:
			numberChild[i[0] - 1] += 1
	for i in range(1, n + 1):
		parent[i] = lst[i - 1][0]
		respect[i] = lst[i - 1][1]
	haveChild = [0] * n
	for i in lst:
		if i[0] != -1:
			haveChild[i[0] - 1] = 1
	noResChild = [0] * n
	for i in range(1, n + 1):
		if parent[i] != -1:
			if respect[parent[i]] == 1 and respect[i] == 1:
				noResChild[parent[i] - 1] += 1
	ansSet = set()
	for i in range(1, n + 1):
		if noResChild[i - 1] == numberChild[i - 1] and noResChild[i - 1] > 0:
			ansSet.add(i)
		if numberChild[i - 1] == 0 and respect[i] == 1:
			ansSet.add(i)
	ansList = sorted(list(ansSet))
	if len(ansList) == 0:
		print(-1)
	else:
		for i in ansList:
			print(i, end=' ')
		print()
n = int(input())
lst = []
for i in range(n):
	x = input()
	if x == '':
		break
	lst.append(list(map(int, x.split())))
solve(n, lst)


from collections import defaultdict
n = int(input())
no_respect = set()
childrencnt = defaultdict(lambda : [0, 0])
parents = {}
root_i = 0
for i in range(1, n + 1):
	(p, c) = (int(p) for p in input().split())
	if p == -1:
		root_i = i
	else:
		childrencnt[p][0] += 1
		parents[i] = p
		if c == 1:
			childrencnt[p][1] += 1
			no_respect.add(i)
ans = []
for i in range(1, n + 1):
	if i == root_i:
		continue
	if i in no_respect and childrencnt[i][0] == childrencnt[i][1]:
		ans.append(i)
		parent = parents[i]
		childrencnt[parent][0] += childrencnt[i][0]
		childrencnt[parent][1] += childrencnt[i][1]
if len(ans) > 0:
	print(' '.join((str(i) for i in ans)))
else:
	print('-1')


N = int(input())
res = [0] * N
deg = [0] * N
root = -1
for i in range(N):
	(p, c) = map(int, input().split())
	if p != -1:
		if c == 1:
			res[i] = 1
		else:
			deg[p - 1] += 1
		deg[i] += 1
	else:
		root = i
ans = []
for i in range(N):
	if deg[i] == 1 and i != root and (res[i] == 1):
		ans.append(i + 1)
if len(ans) == 0:
	print(-1)
else:
	ans = sorted(ans)
	print(*ans)


n = int(input())
c = [0] * n
adj = [[] for i in range(n)]
child = [False] * n
parent = [False] * n
for i in range(n):
	(a, b) = map(int, input().split())
	if b == 0:
		child[i] = True
	if a != -1:
		parent[a - 1] |= child[i]
	else:
		p = i
		c[i] = False
hhk = 0
for i in range(n):
	if child[i] == False and parent[i] == False:
		print(i + 1, end=' ')
		hhk = 1
if hhk == 0:
	print('-1')


from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
p = defaultdict(list)
c = [0] * (n + 1)
for i in range(1, n + 1):
	(a, b) = r()
	p[a] += (i,)
	c[i] = b
arr = []
for i in range(1, n + 1):
	if c[i]:
		f = True
		for k in p[i]:
			if not c[k]:
				f = False
				break
		if f:
			arr.append(i)
if arr:
	print(*arr)
else:
	print(-1)


import sys
import math as mt
I = lambda : list(map(int, input().split()))
(n,) = I()
g = [I() for i in range(n)]
e = [[] for i in range(n + 1)]
for i in range(n):
	if g[i][0] != -1:
		e[g[i][0]].append(i + 1)
ans = []
for i in range(1, n + 1):
	if g[i - 1][1] == 1:
		f = 1
		for j in e[i]:
			if g[j - 1][1] == 0:
				f = 0
				break
		if f:
			ans.append(i)
if ans:
	print(*ans)
else:
	print(-1)


from heapq import heapify, heappush, heappop
from collections import deque
n = int(input())
in_counts = [0] * n
c_counts = [False] * n
parents = [0] * n
childs = [[] for i in range(n)]
for i in range(n):
	(parent, c) = list(map(int, input().split()))
	parents[i] = parent - 1
	c_counts[i] = 1 - c
	if parent > 0:
		in_counts[parent - 1] += c_counts[i]
		childs[parent - 1].append(i)
queue = []
for i in range(n):
	if not c_counts[i] and in_counts[i] == 0:
		queue.append(i + 1)
queue.sort()
if len(queue):
	print(' '.join(list(map(str, queue))))
else:
	print(-1)


import sys
try:
	sys.stdin = open('inputf.in', 'r')
	sys.stdout = open('outputf.in', 'w')
except:
	pass
import math
import sys
from math import sqrt
from collections import defaultdict
from collections import Counter
import string
from collections import deque

def ii():
	return int(sys.stdin.readline().strip())

def si():
	return sys.stdin.readline().strip()

def li():
	return list(map(int, sys.stdin.readline().strip().split()))

def mi():
	return map(int, sys.stdin.readline().strip().split())

def dparr(m, n):
	return [[0] * n for i in range(m)]
n = ii()
graph = {}
c = [0] * (n + 1)
for i in range(1, n + 1):
	graph[i] = []
root = -2
for i in range(1, n + 1):
	(a, b) = mi()
	if a == -1:
		root = i
	else:
		graph[a].append(i)
		c[i] = b
tbd = [False] * (n + 1)
for i in range(1, n + 1):
	x = False
	if c[i] == 1:
		x = True
		for j in graph[i]:
			if c[j] == 0:
				x = False
				break
	tbd[i] = x
ans = []
for i in range(1, len(tbd)):
	if tbd[i]:
		ans.append(i)
if len(ans) == 0:
	print(-1)
else:
	print(*ans)


n = int(input())
g = [True for _ in range(n + 1)]
for i in range(1, n + 1):
	(p, c) = map(int, input().split())
	if c == 0:
		if p != -1:
			g[p] = False
		g[i] = False
ans = list((x for x in range(1, n + 1) if g[x]))
if ans:
	print(*ans)
else:
	print(-1)


n = int(input())
respect = [False] * n
for i in range(n):
	(p, c) = list(map(int, input().split()))
	if p == -1:
		respect[i] = True
	elif c == 0:
		respect[p - 1] = True
		respect[i] = True
disrespect = [str(i + 1) for i in range(n) if not respect[i]]
if len(disrespect) > 0:
	print(' '.join(disrespect))
else:
	print(-1)


n = int(input())
arr = [[0, []] for _ in range(n)]
for idx in range(n):
	(par, res) = map(int, input().split())
	par -= 1
	if par == -2:
		arr[idx][0] = -1
		continue
	arr[par][1].append(idx)
	arr[idx][0] = res
res = []
for (idx, ar) in enumerate(arr):
	if ar[0] != 1:
		continue
	for child in ar[1]:
		if not arr[child][0]:
			break
	else:
		res.append(idx + 1)
if res:
	print(*sorted(res))
else:
	print('-1')


def bs(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return 1
		elif arr[mid] > x:
			return bs(arr, l, mid - 1, x)
		else:
			return bs(arr, mid + 1, r, x)
	else:
		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))


import sys
n = int(input())
parents = [None for i in range(n + 1)]
respects = [None for i in range(n + 1)]
children = [[] for i in range(n + 1)]
children[0] = None
for vertex in range(1, n + 1):
	(parent, respect) = map(int, input().split())
	parents[vertex] = parent
	respects[vertex] = respect
	if parent != -1:
		children[parent].append(vertex)
res = []
for vertex in range(1, n + 1):
	if respects[vertex] == 1 and all((respects[child] == 1 for child in children[vertex])):
		res.append(str(vertex))
print(' '.join(res) or -1)


n = int(input())
cval = [0 for i in range(n)]
par = []
d = [1 for i in range(n)]
for i in range(n):
	(p, c) = map(int, input().split())
	par.append(p)
	cval[i] = c
for i in range(n):
	if par[i] != -1:
		d[par[i] - 1] = d[par[i] - 1] & cval[i]
deleted = False
for i in range(n):
	if d[i] == 1 and cval[i] == 1:
		deleted = True
		print(i + 1, end=' ')
if deleted == False:
	print(-1)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	e = collections.defaultdict(int)
	k = []
	for i in range(1, n + 1):
		(p, c) = LI()
		if c == 1 and p >= 0:
			k.append(i)
		else:
			e[p] += 1
	r = []
	for c in k:
		if e[c] == 0:
			r.append(c)
	if len(r) == 0:
		return -1
	return ' '.join(map(str, r))
print(main())


from collections import defaultdict, Counter
n = int(input())
l = []
p = [0] * n
c = [0] * n
for i in range(n):
	(pp, cc) = map(int, input().split())
	(p[i], c[i]) = (pp, cc)
d = c[:]
for i in range(n):
	if p[i] != -1 and (not c[i]):
		d[p[i] - 1] = 0
if d == [0] * n:
	print(-1)
else:
	for (i, v) in enumerate(d):
		if v:
			print(i + 1, end=' ')


n = int(input())
l = [[] for i in range(n + 1)]
s = []
l2 = [0] * (n + 1)
l3 = [0] * (n + 1)
for i in range(1, n + 1):
	(a, b) = map(int, input().split())
	l2[i] = a
	l3[i] = b
l4 = l3.copy()
for i in range(1, n + 1):
	if l3[i] == 0 and l2[i] != -1:
		l4[l2[i]] = 0
tt = 0
for i in range(1, n + 1):
	if l4[i] == 1:
		print(i, end=' ')
		tt = 1
if tt == 0:
	print(-1)


n = int(input())
sd = dict()
lt = [0] * (n + 1)
for i in range(1, n + 1):
	(a, b) = map(int, input().split())
	if a != -1:
		if a not in sd:
			sd[a] = []
		sd[a].append(i)
	if b == 1:
		lt[i] = 1
an = []
for j in range(1, n + 1):
	if lt[j] == 1:
		if j in sd:
			for k in sd[j]:
				if lt[k] == 0:
					break
			else:
				an.append(j)
		else:
			an.append(j)
if len(an) == 0:
	print('-1')
else:
	print(*an)


n = int(input())
p = [0] * (n + 1)
c = [0] * (n + 1)
e = []
for i in range(n + 1):
	e.append([])
racine = 0
for i in range(1, n + 1):
	(pi, ci) = list(map(int, str(input()).split()))
	if pi == -1:
		racine = i
	else:
		e[pi].append(i)
	c[i] = ci
	p[i] = pi
no = []
for i in range(1, n + 1):
	if c[i] == 1:
		if len(e) == 0 or all((c[x] == 1 for x in e[i])):
			no.append(i)
if len(no) == 0:
	print(-1)
else:
	print(' '.join((str(x) for x in sorted(no))))