Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Topological Sort

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.

Input

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

Output

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

Constraints

$1 \leq |V| \leq 10,000$

$0 \leq |E| \leq 100,000$

There are no parallel edges in $G$

There are no self loops in $G$

Sample Input

6 6
0 1
1 2
3 1
3 4
4 5
5 2

Sample Output

0
3
1
4
5
2

Solution in Rust (judge verdict: Accepted)

use std::io::{self, Read};
use std::str::FromStr;
pub struct Scanner<R: Read> {
    reader: R,
}
impl<R: Read> Scanner<R> {
    pub fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader }
    }
    pub fn read<T: FromStr>(&mut self) -> T {
        let s = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.expect("failed to read char") as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        s.parse::<T>().ok().expect("failed to parse token")
    }
}

use std::collections::VecDeque;

// 幅優先探索によるトポロジカルソート
fn toploogical_sort(v_num: usize, edges: &Vec<(usize, usize)>) -> Vec<usize> {
    let mut result = Vec::with_capacity(v_num);
    let mut in_num = vec![0; v_num];    // 頂点に対する入力辺の数を表す

    // 入力辺の数を数える
    for &(_,t) in edges.iter() {
        in_num[t] += 1;
    }

    // 入力数0の頂点をQueueに
    let mut que = VecDeque::new();
    for i in 0..v_num {
        if in_num[i] == 0 {
            que.push_back(i);
        }
    }

    // 幅優先探索で入力数0のものから順にresultに追加
    while let Some(v) = que.pop_front() {
        result.push(v);
        // 頂点vから繋がる頂点の入力数を1減らす
        for &(s,t) in edges.iter() {
            if s == v {
                in_num[t] -= 1;
                // 減らした際に入力数が0となったら、Queueに追加する
                if in_num[t] == 0 {
                    que.push_back(t);
                }
            }
        }
    }
    result
}


fn main() {
    let sin = io::stdin();
    let sin = sin.lock();
    let mut sc = Scanner::new(sin);

    let v: usize = sc.read();
    let e: usize = sc.read();
    let edges: Vec<(usize, usize)> = (0..e)
        .map(|_| {
            (sc.read::<usize>(), sc.read::<usize>())
        }).collect();

    let results = toploogical_sort(v, &edges);
    
    for v in results {
        println!("{}", v);
    }
}