def main():

    nvertices, nedges = list(map(int, input().split()))

    indegrees = [0 for i in range(nvertices)]

    adj = [[] for i in range(nvertices)]

    for i in range(nedges):

        s, t = list(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()
def main():

    nvertices, nedges = list(map(int, input().split()))

    Adj = [[] for i in range(nvertices)]

    for i in range(nedges):

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

        Adj[s].append(t)



    visited = [False] * nvertices

    ordering = []

    for u in range(nvertices):

        if not visited[u]:

            dfs(u, Adj, visited, ordering)



    for v in reversed(ordering):

        print(v)





def dfs(u, Adj, visited, ordering):

    visited[u] = True

    for v in Adj[u]:

        if not visited[v]:

            dfs(v, Adj, visited, ordering)

    ordering.append(u)





main()
def main():
    nvertices, nedges = list(map(int, input().split()))
    indegrees = [0 for i in range(nvertices)]
    adj = [[] for i in range(nvertices)]
    for i in range(nedges):
        s, t = list(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()

def main():
    nvertices, nedges = list(map(int, input().split()))
    Adj = [[] for i in range(nvertices)]
    for i in range(nedges):
        s, t = list(map(int, input().split()))
        Adj[s].append(t)
    visited = [False] * nvertices
    ordering = []
    for u in range(nvertices):
        if not visited[u]:
            dfs(u, Adj, visited, ordering)
    for v in reversed(ordering):
        print(v)


def dfs(u, Adj, visited, ordering):
    visited[u] = True
    for v in Adj[u]:
        if not visited[v]:
            dfs(v, Adj, visited, ordering)
    ordering.append(u)


main()

-    indegrees = [0 for i in range(nvertices)] -    adj = [[] for i in range(nvertices)] +    Adj = [[] for i in range(nvertices)] -        adj[s].append(t) -        indegrees[t] += 1 -    S = [] -    for i in range(nvertices): -        if indegrees[i] == 0: -            S.append(i) +        Adj[s].append(t) +    visited = [False] * nvertices -    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: +    for u in range(nvertices): +        if not visited[u]: +            dfs(u, Adj, visited, ordering) +    for v in reversed(ordering): +def dfs(u, Adj, visited, ordering): +    visited[u] = True +    for v in Adj[u]: +        if not visited[v]: +            dfs(v, Adj, visited, ordering) +    ordering.append(u) + +