#!/usr/bin/env python

# -*- coding: utf-8 -*-



from sys import stdin

from collections import deque



UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2





def generate_adj_matrix(_v_info):

    for v_detail in _v_info:

        v_from, v_to = list(map(int, v_detail))

        init_adj_table[v_from].append(v_to)

        init_in_deg_list[v_to] += 1

    return init_adj_table, init_in_deg_list





def topological_sort():

    for v in range(vertices):

        if not in_deg_list[v] and status[v] is UNVISITED:

            bfs(v)



    return ans





def bfs(v):

    v_queue.append(v)

    status[v] = VISITED_IN_QUEUE



    while v_queue:

        current = v_queue.popleft()



        ans.append(current)



        for v_adj in adj_table[current]:

            in_deg_list[v_adj] -= 1

            if not in_deg_list[v_adj] and status[v_adj] is UNVISITED:

                status[v_adj] = VISITED_IN_QUEUE

                v_queue.appendleft(v_adj)



        status[current] = POPPED_OUT



    return None





if __name__ == '__main__':

    _input = stdin.readlines()

    vertices, edges = list(map(int, _input[0].split()))

    v_info = [x.split() for x in _input[1:]]



    status = [UNVISITED] * vertices

    v_queue = deque()

    init_adj_table = tuple([] for _ in range(vertices))

    init_in_deg_list = [0] * vertices



    ans = []

    adj_table, in_deg_list = generate_adj_matrix(v_info)

    print((*topological_sort()))
#!/usr/bin/env python

# -*- coding: utf-8 -*-



from sys import stdin

from collections import deque



UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2





def generate_adj_matrix(_v_info):

    for v_detail in _v_info:

        v_from, v_to = map(int, v_detail)

        init_adj_table[v_from].append(v_to)

        init_in_deg_list[v_to] += 1

    return init_adj_table, init_in_deg_list





def topological_sort():

    for v in range(vertices):

        if not in_deg_list[v] and status[v] is UNVISITED:

            bfs(v)



    return ans





def bfs(v):

    v_queue.append(v)

    status[v] = VISITED_IN_QUEUE



    while v_queue:

        current = v_queue.popleft()



        ans.append(current)



        for v_adj in adj_table[current]:

            in_deg_list[v_adj] -= 1

            if not in_deg_list[v_adj] and status[v_adj] is UNVISITED:

                status[v_adj] = VISITED_IN_QUEUE

                v_queue.appendleft(v_adj)



        status[current] = POPPED_OUT



    return None





if __name__ == '__main__':

    _input = stdin.readlines()

    vertices, edges = map(int, _input[0].split())

    v_info = map(lambda x: x.split(), _input[1:])



    status = [UNVISITED] * vertices

    v_queue = deque()

    init_adj_table = tuple([] for _ in range(vertices))

    init_in_deg_list = [0] * vertices



    ans = []

    adj_table, in_deg_list = generate_adj_matrix(v_info)

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import stdin
from collections import deque

UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(_v_info):
    for v_detail in _v_info:
        v_from, v_to = list(map(int, v_detail))
        init_adj_table[v_from].append(v_to)
        init_in_deg_list[v_to] += 1
    return init_adj_table, init_in_deg_list


def topological_sort():
    for v in range(vertices):
        if not in_deg_list[v] and status[v] is UNVISITED:
            bfs(v)
    return ans


def bfs(v):
    v_queue.append(v)
    status[v] = VISITED_IN_QUEUE
    while v_queue:
        current = v_queue.popleft()
        ans.append(current)
        for v_adj in adj_table[current]:
            in_deg_list[v_adj] -= 1
            if not in_deg_list[v_adj] and status[v_adj] is UNVISITED:
                status[v_adj] = VISITED_IN_QUEUE
                v_queue.appendleft(v_adj)
        status[current] = POPPED_OUT
    return None


if __name__ == "__main__":
    _input = stdin.readlines()
    vertices, edges = list(map(int, _input[0].split()))
    v_info = [x.split() for x in _input[1:]]
    status = [UNVISITED] * vertices
    v_queue = deque()
    init_adj_table = tuple([] for _ in range(vertices))
    init_in_deg_list = [0] * vertices
    ans = []
    adj_table, in_deg_list = generate_adj_matrix(v_info)
    print((*topological_sort()))

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import stdin
from collections import deque

UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(_v_info):
    for v_detail in _v_info:
        v_from, v_to = map(int, v_detail)
        init_adj_table[v_from].append(v_to)
        init_in_deg_list[v_to] += 1
    return init_adj_table, init_in_deg_list


def topological_sort():
    for v in range(vertices):
        if not in_deg_list[v] and status[v] is UNVISITED:
            bfs(v)
    return ans


def bfs(v):
    v_queue.append(v)
    status[v] = VISITED_IN_QUEUE
    while v_queue:
        current = v_queue.popleft()
        ans.append(current)
        for v_adj in adj_table[current]:
            in_deg_list[v_adj] -= 1
            if not in_deg_list[v_adj] and status[v_adj] is UNVISITED:
                status[v_adj] = VISITED_IN_QUEUE
                v_queue.appendleft(v_adj)
        status[current] = POPPED_OUT
    return None


if __name__ == "__main__":
    _input = stdin.readlines()
    vertices, edges = map(int, _input[0].split())
    v_info = map(lambda x: x.split(), _input[1:])
    status = [UNVISITED] * vertices
    v_queue = deque()
    init_adj_table = tuple([] for _ in range(vertices))
    init_in_deg_list = [0] * vertices
    ans = []
    adj_table, in_deg_list = generate_adj_matrix(v_info)
    print(*topological_sort(), sep="\n")

-        v_from, v_to = list(map(int, v_detail)) +        v_from, v_to = map(int, v_detail) -    vertices, edges = list(map(int, _input[0].split())) -    v_info = [x.split() for x in _input[1:]] +    vertices, edges = map(int, _input[0].split()) +    v_info = map(lambda x: x.split(), _input[1:]) -    print((*topological_sort())) +    print(*topological_sort(), sep="\n")