#include<bits/stdc++.h>
using namespace std;

int V, E;
vector<set<int>> redge;

set<int> memo;
void topological_sort(int u) {
  if(memo.count(u)) return;
  memo.insert(u);
  for(int v: redge[u]) topological_sort(v);
  cout << u << endl;
}

int main() {
  cin >> V >> E;
  redge.resize(V);
  for(int i = 0; i < E; ++i) {
    int u, v;
    cin >> u >> v;
    redge[v].insert(u);
  }
  for(int i = 0; i < V; ++i) topological_sort(i);
}