def bfs(s):

    que = [s]

    used[s] = True

    while que:

        u = que.pop(0)

        ans.append(u)



        for v in G[u]:

            indeg[v] -= 1

            if indeg[v] == 0 and not used[v]:

                used[v] = True

                que.append(v)



def tsort():

    for u in range(V):

        if indeg[u] == 0 and not used[u]:

            bfs(u)



V, E = list(map(int, input().split()))

indeg = [0] * V

used = [False] * V

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

ans = []



for i in range(E):

    s, t = list(map(int, input().split()))

    indeg[t] += 1

    G[s].append(t)



tsort()

for i in ans:

    print(i)
def tsort(G):

    def bfs(s):

        que = [s]

        used[s] = True

        while que:

            u = que.pop(0)

            res.append(u)



            for v in G[u]:

                indeg[v] -= 1

                if indeg[v] == 0 and not used[v]:

                    used[v] = True

                    que.append(v)



    indeg = [0] * V

    used = [False] * V

    res = []

    for u in range(V):

        for v in G[u]:

            indeg[v] += 1

    for u in range(V):

        if indeg[u] == 0 and not used[u]:

            bfs(u)

    return res





V, E = list(map(int, input().split()))

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

for i in range(E):

    s, t = list(map(int, input().split()))

    G[s].append(t)



ans = tsort(G)

for i in ans:

    print(i)
def bfs(s):
    que = [s]
    used[s] = True
    while que:
        u = que.pop(0)
        ans.append(u)
        for v in G[u]:
            indeg[v] -= 1
            if indeg[v] == 0 and not used[v]:
                used[v] = True
                que.append(v)


def tsort():
    for u in range(V):
        if indeg[u] == 0 and not used[u]:
            bfs(u)


V, E = list(map(int, input().split()))
indeg = [0] * V
used = [False] * V
G = [[] for i in range(V)]
ans = []
for i in range(E):
    s, t = list(map(int, input().split()))
    indeg[t] += 1
    G[s].append(t)
tsort()
for i in ans:
    print(i)

def tsort(G):
    def bfs(s):
        que = [s]
        used[s] = True
        while que:
            u = que.pop(0)
            res.append(u)
            for v in G[u]:
                indeg[v] -= 1
                if indeg[v] == 0 and not used[v]:
                    used[v] = True
                    que.append(v)

    indeg = [0] * V
    used = [False] * V
    res = []
    for u in range(V):
        for v in G[u]:
            indeg[v] += 1
    for u in range(V):
        if indeg[u] == 0 and not used[u]:
            bfs(u)
    return res


V, E = list(map(int, input().split()))
G = [[] for i in range(V)]
for i in range(E):
    s, t = list(map(int, input().split()))
    G[s].append(t)
ans = tsort(G)
for i in ans:
    print(i)

-def bfs(s): -    que = [s] -    used[s] = True -    while que: -        u = que.pop(0) -        ans.append(u) +def tsort(G): +    def bfs(s): +        que = [s] +        used[s] = True +        while que: +            u = que.pop(0) +            res.append(u) +            for v in G[u]: +                indeg[v] -= 1 +                if indeg[v] == 0 and not used[v]: +                    used[v] = True +                    que.append(v) + +    indeg = [0] * V +    used = [False] * V +    res = [] +    for u in range(V): -            indeg[v] -= 1 -            if indeg[v] == 0 and not used[v]: -                used[v] = True -                que.append(v) - - -def tsort(): +            indeg[v] += 1 +    return res -indeg = [0] * V -used = [False] * V -ans = [] -    indeg[t] += 1 -tsort() +ans = tsort(G)