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

from sys import stdin

from collections import deque

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

adj_list = [[] for _ in [0] * V]

is_visited = [False] * V

for _ in [0] * E:

    s, t = map(int, stdin.readline().split())

    adj_list[s].append(t)

out = 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')

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

from sys import stdin
from collections import deque

V, E = map(int, stdin.readline().split())
adj_list = [[] for _ in [0] * V]
is_visited = [False] * V
for _ in [0] * E:
    s, t = map(int, stdin.readline().split())
    adj_list[s].append(t)
out = 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")

-n, e = map(int, stdin.readline().split()) -G = [[] for _ in [0] * n] -indeg = [0] * n -out = [] -V = [False] * n +V, E = map(int, stdin.readline().split()) +adj_list = [[] for _ in [0] * V] +is_visited = [False] * V +for _ in [0] * E: +    s, t = map(int, stdin.readline().split()) +    adj_list[s].append(t) +out = deque() -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 dfs(u): +    is_visited[u] = True +    for v in adj_list[u]: +        if not is_visited[v]: +            dfs(v) +    out.appendleft(u) -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() +for s in range(V): +    if not is_visited[s]: +        dfs(s) +print(*out, sep="\n")