#include <stdio.h>
#include <string.h>
struct node{
    int to,next;
}e[100100];
int cnt=0,head[10100],vis[10100],stack[10100],top=0;
void addedge(int u,int v){
    cnt++,e[cnt].to=v,e[cnt].next=head[u],head[u]=cnt;
}
void dfs(int x){
    int b,v;
    vis[x]=1;
    for(b=head[x];b;b=e[b].next){
        v=e[b].to;
        if(vis[v]==0)
            dfs(v);
    }
    stack[++top]=x;//1 此?写成 stack[++top]=v; ?了会，真是大失水准 
}
int main(){
    int n,m,i,j,u,v;
    memset(head,0,sizeof(head));
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        addedge(u,v);
    }
    for(i=0;i<n;i++)
        if(vis[i]==0)
            dfs(i);
    while(top){
        printf("%d\n",stack[top--]);
    }
    return 0;
}