<image>


A directed acyclic graph (DAG) can be used to represent the ordering of tasks. Tasks are represented by vertices and constraints where one task can begin before another, are represented by edges. For example, in the above example, you can undertake task B after both task A and task B are finished. You can obtain the proper sequence of all the tasks by a topological sort.

Given a DAG $G$, print the order of vertices after the topological sort.

Constraints

* $1 \leq |V| \leq 10,000$
* $0 \leq |E| \leq 100,000$
* There are no parallel edges in $G$
* There are no self loops in $G$

Input

A directed graph $G$ is given in the following format:
$|V|\;|E|$
$s_0 \; t_0$
$s_1 \; t_1$
:
$s_{|E|-1} \; t_{|E|-1}$



$|V|$ is the number of vertices and $|E|$ is the number of edges in the graph. The graph vertices are named with the numbers $0, 1,..., |V|-1$ respectively.

$s_i$ and $t_i$ represent source and target nodes of $i$-th edge (directed).

Output

Print the vertices numbers in order. Print a number in a line.

If there are multiple possible solutions, print any one of them (the solution is judged by a special validator).

Example

Input

6 6
0 1
1 2
3 1
3 4
4 5
5 2


Output

0
3
1
4
5
2

Difficulty: UNKNOWN_DIFFICULTY

Solutions:
from collections import deque
(n, m) = [int(s) for s in input().split()]
E = [set() for _ in range(n)]
for _ in range(m):
	(s, t) = [int(s) for s in input().split(' ')]
	E[s].add(t)
color = [0] * n
indeg = [0] * n
for s in range(n):
	for t in E[s]:
		indeg[t] += 1
Q = deque()
out = []

def bfs(s):
	Q.append(s)
	color[s] = 1
	while Q:
		u = Q.popleft()
		out.append(u)
		for v in E[u]:
			indeg[v] -= 1
			if indeg[v] == 0 and color[v] == 0:
				color[v] = 1
				Q.append(v)
for u in range(n):
	if indeg[u] == 0 and color[u] == 0:
		bfs(u)
print('\n'.join(map(str, out)))


from collections import deque
(V, E) = map(int, input().split())
G = [[] for _ in range(V)]
reached = [False] * V
indeg = [0] * V
for _ in range(E):
	(s, t) = map(int, input().split())
	G[s].append(t)
	indeg[t] += 1
out = []

def bfs(s):
	Q = deque()
	Q.append(s)
	reached[s] = True
	while Q:
		u = Q.popleft()
		out.append(u)
		for v in G[u]:
			indeg[v] -= 1
			if indeg[v] == 0 and reached[v] == False:
				reached[v] = True
				Q.append(v)

def topologicalSort():
	for u in range(V):
		if indeg[u] == 0 and reached[u] == False:
			bfs(u)
topologicalSort()
for i in out:
	print(i)


from sys import stdin
from collections import deque

def read_graph(n, k):
	A = [[] for _ in range(n)]
	for _ in range(k):
		line = stdin.readline().strip().split()
		A[int(line[0])].append(int(line[1]))
	return A

def topological_sort(n, A):
	color = ['WHITE'] * n
	Q = deque([])
	out = []
	indeg = [0] * n
	for i in range(n):
		for j in A[i]:
			indeg[j] = indeg[j] + 1

	def bfs(s):
		nonlocal color
		nonlocal indeg
		nonlocal Q
		color[s] = 'GRAY'
		Q.append(s)
		while Q:
			u = Q.popleft()
			out.append(u)
			for v in A[u]:
				indeg[v] = indeg[v] - 1
				if indeg[v] == 0 and color[v] == 'WHITE':
					color[v] = 'GRAY'
					Q.append(v)
	for i in range(n):
		if indeg[i] == 0 and color[i] == 'WHITE':
			bfs(i)
	return out
(n, k) = [int(i) for i in input().split()]
A = read_graph(n, k)
B = topological_sort(n, A)
for i in B:
	print(i)


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time

def I():
	return int(input())

def S():
	return input()

def MI():
	return map(int, input().split())

def MS():
	return map(str, input().split())

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

def LI_():
	return [int(i) - 1 for i in input().split()]

def StoI():
	return [ord(i) - 97 for i in input()]

def ItoS(nn):
	return chr(nn + 97)

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

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

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10 ** 9 + 7
inf = float('inf')
IINF = 10 ** 19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10 ** 6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False

def main():
	(V, E) = MI()
	edges = []
	for i in range(E):
		edges.append(tuple(MI()))
	in_cnt = defaultdict(int)
	outs = defaultdict(list)
	for (a, b) in edges:
		in_cnt[b] += 1
		outs[a].append(b)
	res = []
	queue = deque([i for i in range(V) if in_cnt[i] == 0])
	while len(queue) != 0:
		v = queue.popleft()
		res.append(v)
		for nxt_v in outs[v]:
			in_cnt[nxt_v] -= 1
			if in_cnt[nxt_v] == 0:
				queue.append(nxt_v)
	for i in range(V):
		print(res[i])
main()


from collections import deque
(V, E) = list(map(int, input().split()))
edge = [[] for i in range(V)]
in_num = [0] * V
for i in range(E):
	(s, t) = list(map(int, input().split()))
	edge[s].append(t)
	in_num[t] += 1
q = deque()
for i in range(V):
	if in_num[i] == 0:
		q.append(i)
ans = deque()
while q:
	v = q.popleft()
	ans.append(v)
	for u in edge[v]:
		in_num[u] -= 1
		if in_num[u] == 0:
			q.append(u)
for a in ans:
	print(a)


from collections import deque
(v, e) = map(int, input().split())
edges = [[] for _ in range(v)]
in_num = [0 for _ in range(v)]
for _ in range(e):
	(s, t) = map(int, input().split())
	edges[s].append(t)
	in_num[t] += 1
q = deque()
for i in range(len(in_num)):
	if in_num[i] == 0:
		q.append(i)
ans = []
while q:
	node = q.popleft()
	ans.append(node)
	for v in edges[node]:
		in_num[v] -= 1
		if in_num[v] == 0:
			q.append(v)
for i in ans:
	print(i)


import queue
(V, E) = map(int, input().split())
G = [[] for _ in range(V)]
visited = [False] * V
indeg = [0] * V
out = []
for _ in range(E):
	(s, t) = map(int, input().split())
	G[s].append(t)
for i in range(V):
	for j in range(len(G[i])):
		indeg[G[i][j]] += 1
q = queue.Queue()
for i in range(V):
	if indeg[i] == 0 and (not visited[i]):
		q.put(i)
		visited[i] = True
		while not q.empty():
			u = q.get()
			out.append(u)
			for j in range(len(G[u])):
				v = G[u][j]
				indeg[v] -= 1
				if indeg[v] == 0 and (not visited[v]):
					visited[v] = True
					q.put(v)
for el in out:
	print(el)


import sys
sys.setrecursionlimit(10 ** 9)

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

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(map(int, input().split()))
INF = float('inf')

def topological_sort(N: int, edges: list) -> list:
	incnts = [0] * N
	outnodes = [[] for i in range(N)]
	for i in range(len(edges)):
		incnts[edges[i][1]] += 1
		outnodes[edges[i][0]].append(edges[i][1])
	S = set()
	for i in range(N):
		if incnts[i] == 0:
			S.add(i)
	L = []
	while S:
		L.append(S.pop())
		for node in outnodes[L[-1]]:
			incnts[node] -= 1
			if incnts[node] == 0:
				S.add(node)
	return L
(N, M) = MAP()
edges = [None] * M
for i in range(M):
	(s, t) = MAP()
	edges[i] = (s, t)
ans = topological_sort(N, edges)
for i in range(N):
	print(ans[i])


import array
import heapq

def topological_sort(num_vs, adj_vs):
	in_deg = array.array('L', (0 for _ in range(num_vs)))
	for dests in adj_vs:
		for dest in dests:
			in_deg[dest] += 1
	sorted_vs = array.array('L')
	pq = []
	for s in range(num_vs):
		if in_deg[s] == 0:
			heapq.heappush(pq, s)
	while pq:
		u = heapq.heappop(pq)
		sorted_vs.append(u)
		for v in adj_vs[u]:
			in_deg[v] -= 1
			if in_deg[v] == 0:
				heapq.heappush(pq, v)
	if all((x == 0 for x in in_deg)):
		return sorted_vs
	else:
		raise TopologicalSortError('The graph has a cycle')

def main():
	(v, e) = map(int, input().split())
	adj_vs = [set() for _ in range(v)]
	for _ in range(e):
		(s, t) = map(int, input().split())
		adj_vs[s].add(t)
	for u in topological_sort(v, adj_vs):
		print(u)
main()


from collections import deque

def inpl():
	return tuple(map(int, input().split()))
(V, E) = inpl()
indeg = [0] * V
adj = {k: [] for k in range(V)}
for _ in range(E):
	(s, t) = inpl()
	adj[s].append(t)
	indeg[t] += 1
Q = deque([i for (i, ind) in enumerate(indeg) if ind == 0])
res = []
while Q:
	q = Q.popleft()
	for i in adj[q]:
		indeg[i] -= 1
		if indeg[i] == 0:
			Q.append(i)
	res.append(q)
for r in res:
	print(r)


import sys, collections as cl, bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize

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

def m():
	return map(int, input().split())

def onem():
	return int(input())

def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x) - 1):
		if aa == x[i + 1]:
			a.append([aa, su])
			aa = x[i + 1]
			su = 1
		else:
			su += 1
	a.append([aa, su])
	return a

def jo(x):
	return ' '.join(map(str, x))

def max2(x):
	return max(map(max, x))

def Topo_sort(x, y):
	co = cl.Counter((n for m in x.values() for n in m))
	S = cl.deque([i for i in range(y) if co[i] == 0])
	while S:
		n = S.popleft()
		yield n
		for i in x[n]:
			co[i] -= 1
			if co[i] <= 0:
				S.append(i)
(v, e) = m()
g = cl.defaultdict(set)
for i in range(e):
	(s, t) = m()
	g[s] |= {t}
a = Topo_sort(g, v)
for i in Topo_sort(g, v):
	print(i)


from collections import Counter
from collections import deque

def topological_sort(g, v):
	in_count = Counter((y for x in g.values() for y in x))
	s = deque([i for i in range(v) if in_count[i] == 0])
	while s:
		n = s.popleft()
		yield n
		for m in g[n]:
			in_count[m] -= 1
			if in_count[m] <= 0:
				s.append(m)
from sys import stdin
from collections import defaultdict
readline = stdin.readline
(v, e) = map(int, readline().split())
g = defaultdict(set)
for _ in range(e):
	(s, t) = map(int, readline().split())
	g[s] |= {t}
for i in topological_sort(g, v):
	print(i)


from collections import deque

def bfs(s, g, color, indeg, out):
	q = deque()
	q.append(s)
	color[s] = True
	while len(q) != 0:
		u = q.popleft()
		out.append(u)
		for i in range(len(g[u])):
			v = g[u][i]
			indeg[v] -= 1
			if indeg[v] == 0 and (not color[v]):
				color[v] = True
				q.append(v)

def tsort(n, g):
	color = [False] * n
	indeg = [0] * n
	for u in range(n):
		for i in range(len(g[u])):
			v = g[u][i]
			indeg[v] += 1
	out = []
	for u in range(n):
		if indeg[u] == 0 and (not color[u]):
			bfs(u, g, color, indeg, out)
	for line in out:
		print(line)
(n, m) = [int(v) for v in input().split()]
g = [[] for i in range(n)]
for i in range(m):
	(s, t) = [int(v) for v in input().split()]
	g[s].append(t)
tsort(n, g)


(nv, ne) = map(int, input().split(' '))
in_degs = [0] * nv
q = []
outs = {}
o = []
for _ in range(ne):
	(s, t) = map(int, input().split(' '))
	if s in outs:
		outs[s].append(t)
	else:
		outs[s] = [t]
	in_degs[t] += 1
for i in range(nv):
	if in_degs[i] == 0:
		q.append(i)
while len(q) > 0:
	v = q.pop(0)
	o.append(v)
	if v in outs:
		for out in outs[v]:
			in_degs[out] -= 1
			if in_degs[out] == 0:
				q.append(out)
[print(v) for v in o]


from collections import deque
(n, k) = map(int, input().split())
a = [[] for i in range(n)]
in_num = [0] * n
for _ in range(k):
	(p, q) = map(int, input().split())
	a[p] += [q]
	in_num[q] += 1
color = [0] * n
result = deque()
aa = deque()

def topologicalSort_w(s):
	aa.append(s)
	color[s] = 1
	while aa:
		u = aa.popleft()
		result.append(u)
		for v in a[u]:
			in_num[v] -= 1
			if in_num[v] == 0 and color[v] == 0:
				color[v] = 1
				aa.append(v)
for i in range(n):
	if in_num[i] == 0 and color[i] == 0:
		topologicalSort_w(i)
for i in range(len(result)):
	print(result.popleft())


def main():
	(nvertices, nedges) = map(int, input().split())
	indegrees = [0 for i in range(nvertices)]
	adj = [[] for i in range(nvertices)]
	for i in range(nedges):
		(s, t) = map(int, input().split())
		adj[s].append(t)
		indegrees[t] += 1
	S = []
	for i in range(nvertices):
		if indegrees[i] == 0:
			S.append(i)
	ordering = []
	while S:
		u = S.pop(0)
		ordering.append(u)
		for v in adj[u]:
			indegrees[v] -= 1
			if indegrees[v] == 0:
				S.append(v)
	for v in ordering:
		print(v)
main()


class Node:

	def __init__(self, id):
		self.id = id
		self.adj = []
		self.indeg = 0
		self.passed = False

	def is_start(self):
		return self.indeg == 0

	def __repr__(self):
		return 'Node({},{},{},{})'.format(self.id, self.passed, [v.id for v in self.adj], [v.id for v in self.radj])

def read_edges(ne, V):
	for _ in range(ne):
		(s, t) = [int(x) for x in input().split()]
		V[s].adj.append(V[t])
		V[t].indeg += 1

def sort(V):
	W = []

	def visit(v):
		S = []
		S.append(v)
		while S:
			v = S.pop()
			v.passed = True
			W.append(v)
			for a in v.adj:
				a.indeg -= 1
				if a.is_start():
					S.append(a)
	for v in V:
		if v.is_start() and (not v.passed):
			visit(v)
	return W

def main():
	(nv, ne) = [int(x) for x in input().split()]
	V = [Node(id) for id in range(nv)]
	read_edges(ne, V)
	W = sort(V)
	[print(v.id) for v in W]
main()


from collections import deque

class Graph:

	def __init__(self, n, edge, indexed=1):
		self.n = n
		self.graph = [[] for _ in range(n)]
		self.rev = [[] for _ in range(n)]
		self.deg = [0 for _ in range(n)]
		for e in edge:
			self.graph[e[0] - indexed].append(e[1] - indexed)
			self.rev[e[1] - indexed].append(e[0] - indexed)
			self.deg[e[1] - indexed] += 1

	def topological_sort(self):
		deg = self.deg[:]
		res = [i for i in range(self.n) if deg[i] == 0]
		queue = deque(res)
		used = [False for _ in range(self.n)]
		while queue:
			node = queue.popleft()
			for adj in self.graph[node]:
				deg[adj] -= 1
				if deg[adj] == 0:
					queue.append(adj)
					res.append(adj)
		return res
import sys
input = sys.stdin.readline
(N, M) = map(int, input().split())
E = [tuple(map(int, input().split())) for _ in range(M)]
g = Graph(N, E, 0)
print('\n'.join(map(str, g.topological_sort())))


from sys import stdin
from collections import deque
(n, e) = map(int, stdin.readline().split())
G = [[] for _ in [0] * n]
indeg = [0] * n
out = []
V = [False] * n

def bfs(s):
	dq = deque([s])
	V[s] = True
	while dq:
		u = dq.popleft()
		out.append(u)
		for i in range(len(G[u])):
			v = G[u][i]
			indeg[v] -= 1
			if indeg[v] == 0 and (not V[v]):
				V[v] = True
				dq.append(v)

def tsort():
	for u in range(n):
		for i in range(len(G[u])):
			indeg[G[u][i]] += 1
	for u in range(n):
		if indeg[u] == 0 and (not V[u]):
			bfs(u)
	print(*out, sep='\n')
for _ in [0] * e:
	(s, t) = map(int, stdin.readline().split())
	G[s].append(t)
tsort()


(V, E) = map(int, input().split())
EDGE = [list(map(int, input().split())) for i in range(E)]
from collections import deque
QUE = deque(list(range(V)))
EDGEOUTLIST = [[] for i in range(V)]
for (x, y) in EDGE:
	EDGEOUTLIST[x].append(y)
check = [0] * V
TOP_SORT = []

def dfs(x):
	if check[x] == 1:
		return
	for to in EDGEOUTLIST[x]:
		if check[to] == 0:
			dfs(to)
	TOP_SORT.append(x)
	check[x] = 1
while QUE:
	x = QUE.pop()
	dfs(x)
TOP_SORT.reverse()
for v in TOP_SORT:
	print(v)


from collections import deque
(v, e) = map(int, input().split())
Edg = [[] for _ in range(v)]
inDeg = [0 for _ in range(v)]
Done = [0 for _ in range(v)]
for _ in range(e):
	(s, t) = map(int, input().split())
	Edg[s].append(t)
	inDeg[t] += 1
Q = deque()
ans = []
for u in range(v):
	if not inDeg[u]:
		Q.append(u)
while Q:
	u = Q.popleft()
	ans.append(u)
	for t in Edg[u]:
		inDeg[t] -= 1
		if inDeg[t] == 0 and (not Done[t]):
			Q.append(t)
			Done[t] = 1
for i in ans:
	print(i)


from collections import namedtuple
DirectedEdge = namedtuple('DirectedEdge', ('src', 'dest'))

def revert(edge):
	return edge._replace(src=edge.dest, dest=edge.src)

class Digraph:

	def __init__(self, v):
		self.v = v
		self._edges = [[] for _ in range(v)]

	def add(self, edge):
		self._edges[edge.src].append(edge)

	def adj(self, v):
		return self._edges[v]

	def edges(self):
		for es in self._edges:
			for e in es:
				yield e

	def reversed(self):
		g = self.__class__(self.v)
		for e in self.edges():
			g.add(revert(e))
		return g

def sorted_topological(graph):
	return reversed(list(reverse_post(graph.reversed())))

def reverse_post(graph):

	def visit(v):
		if not visited[v]:
			visited[v] = True
			return True
		else:
			return False

	def leave(v):
		if not left[v]:
			left[v] = True
			vs.append(v)
		return False
	visited = [False] * graph.v
	left = [False] * graph.v
	vs = []
	for i in range(graph.v):
		if not visited[i]:
			stack = [(i, leave)]
			stack.extend(((e.dest, visit) for e in graph.adj(i)))
			while stack:
				(v, f) = stack.pop()
				if f(v):
					stack.append((v, leave))
					for e in graph.adj(v):
						stack.append((e.dest, visit))
	return reversed(vs)

def run():
	(v, e) = [int(i) for i in input().split()]
	g = Digraph(v)
	for _ in range(e):
		(s, t) = [int(i) for i in input().split()]
		g.add(DirectedEdge(s, t))
	for v in sorted_topological(g):
		print(v)
run()


def topological_sort(G, revG):
	n = len(revG)
	S = set()
	for u in revG:
		if len(revG[u]) == 0:
			S.add(u)
	L = []
	while len(S) > 0:
		u = S.pop()
		L.append(u)
		for v in G[u]:
			revG[v].remove(u)
			if len(revG[v]) == 0:
				S.add(v)
	return L
(n, m) = [int(i) for i in input().strip().split(' ')]
G = {}
revG = {}
for i in range(n):
	G[i] = set()
	revG[i] = set()
for _ in range(m):
	(s, t) = [int(i) for i in input().strip().split(' ')]
	G[s].add(t)
	revG[t].add(s)
L = topological_sort(G, revG)
for n in L:
	print(n)


import sys
import os
import pprint
from queue import Queue
(V, E) = list(map(int, input().split()))
G = [[] for i in range(V)]
indig = [0] * V
for i in range(E):
	(s, t) = list(map(int, input().split()))
	G[s].append(t)
	indig[t] += 1
q = Queue()
out = []

def bfs(node_index):
	q.put(node_index)
	while not q.empty():
		u = q.get()
		out.append(u)
		for index in G[u]:
			indig[index] -= 1
			if indig[index] == 0:
				q.put(index)
indig_copy = indig[:]
for i in range(V):
	if indig_copy[i] == 0:
		bfs(node_index=i)
for v in out:
	print(v)


(v, e) = map(int, input().split())
indeg = [0] * v
uv = [[] for _ in range(v)]
for i in range(e):
	(s, t) = map(int, input().split())
	uv[s].append(t)
	indeg[t] += 1
visited = [False] * v
ans = []
for i in range(v):
	if indeg[i] == 0 and (not visited[i]):
		q = [i]
		visited[i] = True
		while len(q) > 0:
			u = q.pop(0)
			ans.append(u)
			for v in uv[u]:
				indeg[v] -= 1
				if indeg[v] == 0 and (not visited[v]):
					visited[v] = True
					q.append(v)
for v in ans:
	print(v)


from collections import deque

def bfs(s):
	q = deque()
	q.append(s)
	while len(q) > 0:
		u = q.popleft()
		searched[u] = True
		res.append(u)
		for v in adj[u]:
			indeg[v] -= 1
			if indeg[v] == 0 and (not searched[v]):
				q.append(v)
(V, E) = map(int, input().split())
adj = [[] for i in range(V)]
indeg = [0 for i in range(V)]
for i in range(E):
	(u, v) = map(int, input().split())
	adj[u].append(v)
	indeg[v] += 1
searched = [False for i in range(V)]
res = []
for u in range(V):
	if indeg[u] == 0 and (not searched[u]):
		bfs(u)
for u in res:
	print(u)


import sys
(v, e) = map(int, sys.stdin.readline().strip().split())
G = [[] for _ in range(v)]
indeg = [0] * v
for i in range(e):
	(s, t) = map(int, sys.stdin.readline().strip().split())
	G[s].append(t)
	indeg[t] += 1
ans = []

def dfs(u):
	if indeg[u] == 0:
		ans.append(u)
		for i in G[u]:
			indeg[i] -= 1
			dfs(i)
indeg_start = indeg.copy()
for i in range(v):
	if indeg_start[i] == 0:
		dfs(i)
for i in ans:
	print(i)


from collections import deque
from collections import defaultdict
adj = defaultdict(lambda : [])
(n_V, n_E) = list(map(int, input().split()))
indeg = [0] * n_V
for _ in range(n_E):
	(s, t) = list(map(int, input().split()))
	indeg[t] += 1
	adj[s].append(t)

def topological_sort(dag, indeg):
	is_visited = [False] * len(indeg)
	ret = []
	perm = [-1] * len(indeg)
	que = deque()
	for (i, deg) in enumerate(indeg):
		if deg == 0:
			que.append((i, 0))
			is_visited[i] = True
	while que:
		(u, cnt) = que.popleft()
		ret.append(u)
		perm[u] = cnt
		for to in dag[u]:
			indeg[to] -= 1
			if indeg[to] or is_visited[to]:
				continue
			que.append((to, cnt + 1))
			is_visited[to] = True
	if False in is_visited:
		print(is_visited)
		raise ValueError('inputted graph is not DAG')
	return (ret, perm)
(ans, _) = topological_sort(adj, indeg)
print(*ans, sep='\n')


import queue

def bfs(u):
	q = queue.Queue()
	q.put(u)
	while not q.empty():
		v = q.get()
		color[v] = 'black'
		ans.append(v)
		for w in Adj[v]:
			indeg[w] -= 1
			if indeg[w] == 0 and color[w] == 'white':
				q.put(w)
	return
(n, m) = map(int, input().split())
color = ['white' for _ in range(n)]
Adj = [[] for _ in range(n)]
indeg = [0] * n
ans = []
for i in range(m):
	(s, t) = map(int, input().split())
	Adj[s].append(t)
	indeg[t] += 1
for i in range(n):
	if indeg[i] == 0 and color[i] == 'white':
		bfs(i)
for i in range(n):
	print(ans[i])


from collections import defaultdict, deque
(v, n) = map(int, input().split())
es = [[int(x) for x in input().split()] for _ in range(n)]
outs = defaultdict(list)
ins = defaultdict(int)
for (v1, v2) in es:
	outs[v1].append(v2)
	ins[v2] += 1
q = deque((v1 for v1 in range(v) if ins[v1] == 0))
res = []
while q:
	v1 = q.popleft()
	res.append(v1)
	for v2 in outs[v1]:
		ins[v2] -= 1
		if ins[v2] == 0:
			q.append(v2)
for i in res:
	print(i)


(v, e) = map(int, input().split())
adjacency_list = [[] for i in range(v)]
queue = []
deg = [0] * v
for _ in range(e):
	(s, t) = map(int, input().split())
	adjacency_list[s].append(t)
	deg[t] += 1
for i in range(v):
	if deg[i] == 0:
		queue.append(i)
		print(i)
while queue:
	vertex = queue.pop(0)
	for v in adjacency_list[vertex]:
		deg[v] -= 1
		if deg[v] == 0:
			queue.append(v)
			print(v)


import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce

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

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

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

def ZIP(n):
	return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
(V, E) = MAP()
st = [LIST() for _ in range(E)]
graph = [[] for _ in range(V)]
dag = [0] * V
for (s, t) in st:
	graph[s].append(t)
	dag[t] += 1
ans = [i for (i, x) in enumerate(dag) if x == 0]
q = deque([i for (i, x) in enumerate(dag) if x == 0])
while q:
	S = q.popleft()
	for node in graph[S]:
		dag[node] -= 1
		if dag[node] == 0:
			ans.append(node)
			q.append(node)
if sum(dag):
	print(-1)
else:
	print(*ans, sep='\n')


(nv, ne) = map(int, input().split(' '))
in_degs = [0] * nv
q = []
outs = {}
o = []
for _ in range(ne):
	(s, t) = map(int, input().split(' '))
	if s in outs:
		outs[s].append(t)
	else:
		outs[s] = [t]
	in_degs[t] += 1
for i in range(nv):
	if in_degs[i] == 0:
		q.append(i)
while q:
	v = q.pop(0)
	o.append(v)
	if v in outs:
		for out in outs[v]:
			in_degs[out] -= 1
			if in_degs[out] == 0:
				q.append(out)
[print(v) for v in o]


from collections import defaultdict, deque
(v, n) = map(int, input().split())
es = [[int(x) for x in input().split()] for _ in range(n)]
outs = defaultdict(list)
ins = defaultdict(int)
for (v1, v2) in es:
	outs[v1].append(v2)
	ins[v2] += 1
q = deque((v1 for v1 in range(v) if ins[v1] == 0))
res = []
while q:
	v1 = q.popleft()
	res.append(v1)
	for v2 in outs[v1]:
		ins[v2] -= 1
		if ins[v2] == 0:
			q.append(v2)
for i in res:
	print(i)


from collections import deque
(V, E) = map(int, input().split())
g = [[] for _ in range(V)]
in_E = [0 for _ in range(V)]
for _ in range(E):
	(s, t) = map(int, input().split())
	g[s].append(t)
	in_E[t] += 1
d = deque()
for i in range(V):
	if in_E[i] == 0:
		d.append(i)
ans = []
while d:
	v = d.popleft()
	for u in g[v]:
		in_E[u] -= 1
		if in_E[u] == 0:
			d.append(u)
	ans.append(v)
for i in ans:
	print(i)


from collections import deque
(V, E) = map(int, input().split())
dic = {}
pnum = [0] * V
for i in range(E):
	(s, t) = map(int, input().split())
	pnum[t] += 1
	if s not in dic:
		dic[s] = []
	dic[s].append(t)
q = deque([])
ans = []
for i in range(V):
	if pnum[i] == 0:
		q.append(i)
		ans.append(i)
while len(q) > 0:
	now = q.popleft()
	if now not in dic:
		continue
	for i in dic[now]:
		pnum[i] -= 1
		if pnum[i] == 0:
			q.append(i)
			ans.append(i)
for i in range(V):
	print(ans[i])


from collections import deque
(v, e) = map(int, input().split())
adj = [[] for _ in range(v)]
inflow = [0] * v
for _ in range(e):
	(s, t) = map(int, input().split())
	inflow[t] += 1
	adj[s].append(t)

def topological_sort(adj, inflow):
	is_visited = [False] * len(inflow)
	res = []

	def bfs(s):
		q = deque([s])
		is_visited[s] = True
		res.append(s)
		while q:
			p = q.popleft()
			for r in adj[p]:
				inflow[r] -= 1
				if inflow[r] == 0 and (not is_visited[r]):
					q.append(r)
					is_visited[r] = True
					res.append(r)
	for x in range(len(inflow)):
		if inflow[x] == 0 and (not is_visited[x]):
			bfs(x)
	return res
for i in topological_sort(adj, inflow):
	print(i)


import queue
(nv, ne) = map(int, input().split(' '))
in_degs = [0] * nv
q = queue.Queue()
outs = {}
o = []
for _ in range(ne):
	(s, t) = map(int, input().split(' '))
	if s in outs:
		outs[s].append(t)
	else:
		outs[s] = [t]
	in_degs[t] += 1
for i in range(nv):
	if in_degs[i] == 0:
		q.put(i)
while not q.empty():
	v = q.get()
	o.append(v)
	if v in outs:
		for out in outs[v]:
			in_degs[out] -= 1
			if in_degs[out] == 0:
				q.put(out)
[print(v) for v in o]


(V, E) = map(int, input().split())
adjacents = []
for i in range(V):
	adjacents.append([])
for i in range(E):
	(s, t) = map(int, input().split())
	adjacents[t].append(s)

def topological_sort(num, adjacents):
	colors = ['white'] * num
	result = []

	def dfs(u):
		if colors[u] == 'black':
			return
		colors[u] = 'black'
		for a in adjacents[u]:
			if colors[a] != 'black':
				dfs(a)
		result.append(u)
	for n in range(num):
		dfs(n)
	return result
for i in topological_sort(V, adjacents):
	print(i)


import sys, collections
(V, E) = tuple(map(int, sys.stdin.readline().split()))
INF = float('inf')
st = tuple((tuple(map(int, sys.stdin.readline().rstrip().split())) for _ in range(E)))
G = [[] for _ in range(V)]
for (s, t) in st:
	G[s].append(t)
visited = set()
indeg = [0] * V
ret = []

def bfs(s):
	q = collections.deque([s])
	visited.add(s)
	while q:
		u = q.popleft()
		ret.append(u)
		for v in G[u]:
			indeg[v] -= 1
			if indeg[v] == 0 and (not v in visited):
				visited.add(v)
				q.append(v)

def topologicalSort_bfs():
	for a in G:
		for t in a:
			indeg[t] += 1
	for i in range(V):
		if indeg[i] == 0 and (not i in visited):
			bfs(i)

def dfs(s):
	if s in visited:
		return
	for e in G[s]:
		dfs(e)
	ret.append(s)
	visited.add(s)

def topologicalSort_dfs():
	for i in range(V):
		dfs(i)
	ret.reverse()
topologicalSort_bfs()
print(*ret, sep='\n')


import heapq

class TopologicalSortError(Exception):
	pass

def topological_sort(num_vs, adj_vs):
	in_deg = [0 for _ in range(num_vs)]
	for dests in adj_vs:
		for dest in dests:
			in_deg[dest] += 1
	sorted_vs = []
	pq = []
	for s in range(num_vs):
		if in_deg[s] == 0:
			heapq.heappush(pq, s)
	while pq:
		u = heapq.heappop(pq)
		sorted_vs.append(u)
		for v in adj_vs[u]:
			in_deg[v] -= 1
			if in_deg[v] == 0:
				heapq.heappush(pq, v)
	if all((x == 0 for x in in_deg)):
		return sorted_vs
	else:
		raise TopologicalSortError('The graph has a cycle')

def main():
	(v, e) = map(int, input().split())
	adj_vs = [set() for _ in range(v)]
	for _ in range(e):
		(s, t) = map(int, input().split())
		adj_vs[s].add(t)
	for u in topological_sort(v, adj_vs):
		print(u)
main()


from collections import deque
(N, E) = map(int, input().split())
G = [[] for i in range(N)]
In = [0] * N
for i in range(E):
	(s, t) = map(int, input().split())
	G[s].append(t)
	In[t] += 1
S = deque([i for i in range(N) if In[i] == 0])
res = []
while S:
	b = S.popleft()
	for nv in G[b]:
		In[nv] -= 1
		if In[nv] == 0:
			S.append(nv)
	res.append(b)
print(*res, sep='\n')


def main():
	(N, M) = (int(i) for i in input().split())
	G = [[] for _ in range(N)]
	deg = [0] * N
	for _ in range(M):
		(a, b) = (int(i) for i in input().split())
		G[a].append(b)
		deg[b] += 1
	from collections import deque

	def topological_sort():
		que = deque()
		ans = [] * N
		for i in range(N):
			if deg[i] == 0:
				que.append(i)
				ans.append(i)
		while que:
			u = que.popleft()
			for v in G[u]:
				deg[v] -= 1
				if deg[v] == 0:
					que.append(v)
					ans.append(v)
		return ans
	print(*topological_sort(), sep='\n')
main()


import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 1e-09
(V, E) = map(int, input().split())
in_num = [0] * V
G = [[] for i in range(V)]
for loop in range(E):
	(tmp_from, tmp_to) = map(int, input().split())
	G[tmp_from].append(tmp_to)
	in_num[tmp_to] += 1
Q = deque()
for i in range(V):
	if in_num[i] == 0:
		Q.append(i)
while len(Q) > 0:
	node_id = Q.popleft()
	print('%d' % node_id)
	for adj in G[node_id]:
		in_num[adj] -= 1
		if in_num[adj] == 0:
			Q.append(adj)