import sys



f_i = sys.stdin



V, E = map(int, f_i.readline().split())



adj_list = [[] for i in range(V)]



is_visited = [False] * V



indeg = [0] * V



for l_i in f_i:

    s, t = map(int, l_i.split())

    adj_list[s].append(t)

    indeg[t] += 1



import collections



Q = collections.deque()

out = collections.deque()





def bfs(s):

    Q.append(s)

    is_visited[s] = True

    while Q:

        u = Q.popleft()



        out.append(u)



        for v in adj_list[u]:

            indeg[v] -= 1

            if indeg[v] == 0 and is_visited[v] == False:

                is_visited[v] = True

                Q.append(v)





# Topological Sort

for u in range(V):

    if indeg[u] == 0 and is_visited[u] == False:

        bfs(u)



print(*out, sep = '\n')

import sys



f_i = sys.stdin



V, E = map(int, f_i.readline().split())



adj_list = [[] for i in range(V)]



is_visited = [False] * V



for l_i in f_i:

    s, t = map(int, l_i.split())

    adj_list[s].append(t)



import collections



out = collections.deque()



def dfs(u):

    is_visited[u] = True

    for v in adj_list[u]:

        if not is_visited[v]:

            dfs(v)

    out.appendleft(u)



for s in range(V):

    if not is_visited[s]:

        dfs(s)



print(*out, sep = '\n')

import sys

f_i = sys.stdin
V, E = map(int, f_i.readline().split())
adj_list = [[] for i in range(V)]
is_visited = [False] * V
indeg = [0] * V
for l_i in f_i:
    s, t = map(int, l_i.split())
    adj_list[s].append(t)
    indeg[t] += 1
import collections

Q = collections.deque()
out = collections.deque()


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


# Topological Sort
for u in range(V):
    if indeg[u] == 0 and is_visited[u] == False:
        bfs(u)
print(*out, sep="\n")

import sys

f_i = sys.stdin
V, E = map(int, f_i.readline().split())
adj_list = [[] for i in range(V)]
is_visited = [False] * V
for l_i in f_i:
    s, t = map(int, l_i.split())
    adj_list[s].append(t)
import collections

out = collections.deque()


def dfs(u):
    is_visited[u] = True
    for v in adj_list[u]:
        if not is_visited[v]:
            dfs(v)
    out.appendleft(u)


for s in range(V):
    if not is_visited[s]:
        dfs(s)
print(*out, sep="\n")

-indeg = [0] * V -    indeg[t] += 1 -Q = collections.deque() -def bfs(s): -    Q.append(s) -    is_visited[s] = True -    while Q: -        u = Q.popleft() -        out.append(u) -        for v in adj_list[u]: -            indeg[v] -= 1 -            if indeg[v] == 0 and is_visited[v] == False: -                is_visited[v] = True -                Q.append(v) +def dfs(u): +    is_visited[u] = True +    for v in adj_list[u]: +        if not is_visited[v]: +            dfs(v) +    out.appendleft(u) -# Topological Sort -for u in range(V): -    if indeg[u] == 0 and is_visited[u] == False: -        bfs(u) +for s in range(V): +    if not is_visited[s]: +        dfs(s)