#include<iostream>
#include<cstdio>
#include<queue>

using namespace std;

#define rep(i, n) for(i = 0;i < n;++i)
#define pb push_back
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)

struct edge{
  int to;
};

vector<edge> G[10000];
int v, e;

#define maxV 10000
int in[maxV] = {0};
int ord[maxV] = {0};
int topo[maxV] = {0}, topohd = 0;
void toposort(void){
  for(int i = 0;i < v;++i)for(int j = 0;j < G[i].size();++j)++in[G[i][j].to];
  queue<int> Q;
  for(int i = 0;i < v;++i)if(in[i] == 0)Q.push(i);
  for(;!Q.empty();){
    int now = Q.front(); Q.pop();
    topo[topohd++] = now;
    for(int i = 0;i < G[now].size();++i)if(--in[G[now][i].to] == 0)Q.push(G[now][i].to);
  }
  return;
}

signed main(void){
  int i, j;
  scanf("%d%d", &v, &e);
  rep(i, e){
    int2(a, b);
    G[a].pb((edge){b});
  }

  toposort();
  rep(i, v)printf("%d\n", topo[i]);

  return 0;
}