class TopologicalSort

  class Node
    attr_accessor :id, :indeg, :to, :color
    def initialize(id)
      @id = id
      @indeg = 0
      @to = []
      @color = "WHITE"
    end
  end

  def initialize
    v, e = gets.split.map &:to_i
    @nodes = Array.new(v).map.with_index { |n, i| Node.new(i) }
    @result = []
    @q = []
    e.times do
      s, t = gets.split.map &:to_i
      @nodes[t].indeg += 1
      @nodes[s].to << @nodes[t]
    end
  end

  def bfs(s)
    @q.push s
    s.color = "GRAY"
    while !@q.empty?
      u = @q.shift
      @result.push u.id
      u.to.each do |v|
        v.indeg -= 1
        if v.indeg == 0 && v.color == "WHITE"
          v.color = "GRAY"
          @q.push v
        end
      end
    end
  end

  def solve
    @nodes.each do |n|
      bfs(n) if n.indeg == 0 && n.color == "WHITE"
    end
  end

  def dump
    @result.each do |o|
      puts o
    end
  end
end

# main
t = TopologicalSort.new
t.solve
t.dump