#include <stdio.h>          // printf(), scanf()
#include <stdlib.h>         // exit()
#include <stdbool.h>

#define MAX_V 10000
#define MAX_E 100000

typedef struct list_tbl
{
	int t;
	int next;
} list_t;

int V;
int G[MAX_V];
list_t list[MAX_E];
bool visited[MAX_V];
int out[MAX_V];
int ix;

void
cleanup(int ecode)
{
	exit(ecode);
}

void
dfs(int v)
{
	visited[v] = true;
	for (int l = G[v]; l != -1; l = list[l].next)
	{
		int u = list[l].t;
		if (!visited[u])
			dfs(u);
	}

	out[--ix] = v;
}

void
tsort()
{
	for (int v = 0; v < V; ++v)
		visited[v] = false;

	for (int v = 0; v < V; ++v)
	{
		if (!visited[v])
			dfs(v);
	}
}

int
main(int argc, char** argv)
{
	scanf("%d", &V);
	for (int i = 0; i < V; ++i)
		G[i] = -1;

	int E;
	scanf("%d", &E);


	int nl = 0;
	for (int i = 0; i < E; ++i)
	{
		int s, t;
		scanf("%d %d", &s, &t);
		if (nl >= MAX_E)
		{
			fprintf(stderr, "graph area overflow. size:%d\n", MAX_E);
			cleanup(1);
		}

		list[nl].t = t;
		list[nl].next = G[s];
		G[s] = nl++;
	}

	ix = V;
	tsort();

	for (; ix < V; ++ix)
		printf("%d\n", out[ix]);

	return 0;
}