#include <iostream>
#include <iomanip>
#include <limits>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <unordered_map>
#include <random>
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>
#include <cfloat>

using namespace std;

#define INFD numeric_limits<double>::infinity()
#define INFL (int)1e8
#define INFLL (long long)1e15
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/
#include <iostream>
#include <iomanip>
#include <limits>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <unordered_map>
#include <random>
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>
#include <cfloat>

using namespace std;

#define INFD numeric_limits<double>::infinity()
#define INFL (int)1e8
#define INFLL (long long)1e15
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

/*******************************************************/

struct graph_t {
  int n;           // |V|, index begins with 0
  int m;           // |E|
  vector<P> edges; // E
  vector<ll> cost; // cost or distance
  vector<ll> cap;  // capacity
};

class Topological_Sort {
private:
  struct node {
    int id; bool done; vi to; int from_cnt;
  };
  vector<node> nodes;
  int n;
  vi result;
  bool cycle_existing_flag;
public:
  Topological_Sort(graph_t G) {
    n = G.n;
    nodes.resize(n);
    Loop(i, n) nodes[i] = { i, false, {}, 0 };
    Loop(i, G.edges.size()) {
      nodes[G.edges[i].first].to.push_back(G.edges[i].second);
      nodes[G.edges[i].second].from_cnt++;
    }
    result = {};
    stack<int> stk;
    vector<bool> evalid(G.edges.size(), true);
    Loop(i, n) {
      if (nodes[i].from_cnt == 0) stk.push(i);
    }
    while (stk.size()) {
      int a = stk.top(); stk.pop();
      result.push_back(a);
      Loop(i, nodes[a].to.size()) {
        int b = nodes[a].to[i];
        nodes[b].from_cnt--;
        if (nodes[b].from_cnt == 0) stk.push(b);
      }
    }
    if (result.size() != n) cycle_existing_flag = true;
    else cycle_existing_flag = false;
    return;
  }
  vi get_result() {
    return result;
  }
  bool is_cycle_existing() {
    return cycle_existing_flag;
  }
};

int main() {
  graph_t G;
  cin >> G.n >> G.m;
  Loop(i, G.m) {
    int s, t; cin >> s >> t;
    G.edges.push_back({ s, t });
  }
  Topological_Sort ts(G);
  vi result = ts.get_result();
  Loop(i, result.size()) {
    cout << result[i] << endl;
  }
  return 0;
}


Measured memory_bytes: 4288