This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_B"
#include "../memo/macro.hpp"
#include "../graph/topological.cpp"
// スペシャルジャッジだからだめ
signed main(void) {
ll n, m;
cin >> n >> m;
vector<vector<ll>> g(n);
REP(i, m) {
ll u, v;
cin >> u >> v;
g[u].push_back(v);
}
auto ans = tsort(g);
for(auto p: ans) cout << p.first << endl;
return 0;
}
#line 1 "test/GRL4B.memo.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/4/GRL_4_B"
#line 1 "memo/macro.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
const ll INF = 1LL<<60;
#line 1 "graph/topological.cpp"
// BEGIN CUT
// ans[i] = (i番目の頂点v, swapできる頂点をまとめたときにvが何番目か)
vector<PII> tsort(vector<vector<ll>> g) {
const int n = g.size();
vector<ll> h(n);
REP(i, n) for(int j: g[i]) h[j]++;
stack<PII> st;
REP(i, n) if(h[i] == 0) st.push({i, 0});
vector<PII> ans;
while(st.size()) {
PII p = st.top(); st.pop();
ans.push_back(p);
for(auto& j: g[p.first]) {
h[j]--;
if(h[j] == 0) st.push({j, p.second+1});
}
}
return ans;
}
// END CUT
#line 4 "test/GRL4B.memo.cpp"
// スペシャルジャッジだからだめ
signed main(void) {
ll n, m;
cin >> n >> m;
vector<vector<ll>> g(n);
REP(i, m) {
ll u, v;
cin >> u >> v;
g[u].push_back(v);
}
auto ans = tsort(g);
for(auto p: ans) cout << p.first << endl;
return 0;
}