# initialize
@v, e = gets.split.map &:to_i
@indeg = Array.new(@v, 0)
@g = Array.new(@v).map { Array.new(@v, 0) }
@color = Array.new(@v, "WHITE")
@que = []
@out = []

e.times do
  s, t = gets.split.map &:to_i
  @indeg[t] += 1
  @g[s][t] = 1
end

# bfs
def bfs(s)
  @que.push s
  @color[s] = "GRAY"
  while !@que.empty?
    u = @que.shift
    @out.push u

    (0..@v-1).each do |v|
      if @g[u][v] == 1
        @indeg[v] -= 1
        if @indeg[v] == 0 && @color[v] == "WHITE"
          @color[v] = "GRAY"
          @que.push v
        end
      end
    end
  end
end

# main
(0..@v-1).each do |u|
  bfs(u) if @indeg[u] == 0 && @color[u] == "WHITE"
end

@out.each do |o|
  puts o
end