output_description: Print the vertices numbers in order. Print a number in a line.
  
  If there are multiple possible solutions, print any one of them (the solution
  is judged by a special validator).
input_description: A directed graph $G$ is given in the following format:  
  $|V|\;|E|$  
  $s_0 \; t_0$  
  $s_1 \; t_1$  
  :  
  $s_{|E|-1} \; t_{|E|-1}$  
    
  
  $|V|$ is the number of vertices and $|E|$ is the number of edges in the graph.
  The graph vertices are named with the numbers $0, 1,..., |V|-1$ respectively.
  
  $s_i$ and $t_i$ represent source and target nodes of $i$-th edge (directed).
attempt: #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  """
  
  input:
  6 6
  0 1
  1 2
  3 1
  3 4
  4 5
  5 2
  
  output:
  0
  3
  1
  4
  5
  2
  """
  
  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):
      queue = deque()
  
      queue.append(v)
      status[v] = VISITED_IN_QUEUE
  
      while queue:
          current = 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
                  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
      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")
  
problem_description: Topological Sort
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_GRL_tsort)  
  
  A directed acyclic graph (DAG) can be used to represent the ordering of tasks.
  Tasks are represented by vertices and constraints where one task can begin
  before another, are represented by edges. For example, in the above example,
  you can undertake task B after both task A and task B are finished. You can
  obtain the proper sequence of all the tasks by a topological sort.
  
  Given a DAG $G$, print the order of vertices after the topological sort.
samples: [{"input": "6\n    0 1\n    1 2\n    3 1\n    3 4\n    4 5\n    5 2", "output": "3\n    1\n    4\n    5\n    2"}]