#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef struct directed_edge {
  int32_t vertex;
  int32_t next;
} graph_edge;

typedef struct directedGraph {
  graph_edge *edge;
  int32_t *start;
  int32_t pointer;
  int32_t vertex_num;
  int32_t edge_max_size;
} graph;

graph* new_graph (const int vertex_num) {
  graph *g = (graph *) calloc (1, sizeof (graph));
  g->edge = (graph_edge *) calloc (1, sizeof (graph_edge));
  g->start = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  g->pointer = 0;
  g->vertex_num = vertex_num;
  g->edge_max_size = 1;
  for (int32_t i = 0; i < vertex_num; ++i) {
    g->start[i] = -1;
  }
  return g;
}

void add_edge (graph *g, int32_t from, int32_t to) {
  if (g->pointer == g->edge_max_size) {
    g->edge_max_size *= 2;
    g->edge = (graph_edge *) realloc (g->edge, sizeof (graph_edge) * g->edge_max_size);
  }
  g->edge[g->pointer] = (graph_edge) {to, g->start[from]};
  g->start[from] = g->pointer++;
}

int topological_sort (graph *g, int32_t *q) {
  int32_t n = g->vertex_num;
  int32_t *cnt = (int32_t *) calloc (n, sizeof (int32_t));
  for (int32_t i = 0; i < n; ++i) {
    for (int32_t p = g->start[i]; p != -1; p = g->edge[p].next) {
      const int32_t u = g->edge[p].vertex;
      cnt[u]++;
    }
  }
  int32_t front = 0;
  int32_t last = 0;
  for (int32_t i = 0; i < n; ++i) {
    if (cnt[i] == 0) {
      q[last++] = i;
    }
  }
  while (front < last && last < n) {
    const int32_t v = q[front++];
    for (int32_t p = g->start[v]; p != -1; p = g->edge[p].next) {
      const int32_t u = g->edge[p].vertex;
      cnt[u]--;
      if (cnt[u] == 0) {
	q[last++] = u;
      }
    }
  }
  free (cnt);
  return last == n;
}

typedef int32_t i32;

void run (void) {
  i32 v, e;
  scanf ("%" SCNi32 "%" SCNi32, &v, &e);
  graph *g = new_graph (v);
  while (e--) {
    i32 s, t;
    scanf ("%" SCNi32 "%" SCNi32, &s, &t);
    add_edge (g, s, t);
  }
  i32 *q = (i32 *) calloc (v, sizeof (i32));
  topological_sort (g, q);
  for (i32 i = 0; i < v; ++i) {
    printf ("%" PRIi32 "\n", q[i]);
  }
}

int main (void) {
  run ();
  return 0;
}

