require 'objspace'
require 'tsort'
class Hash
  include TSort
  alias tsort_each_node each_key
  def tsort_each_child(node, &block)
    fetch(node).each(&block)
  end
end

n_teams = gets.to_i
games   = gets.to_i

result = {}
n_teams.times{ |team| result[team+1] = [] }

games.times do
  win,lose = gets.split.map(&:to_i)
  result[win] << lose
end

L = result.tsort
LL = []
n_teams.times do
    LL << L.pop
end

# p ObjectSpace.memsize_of(result.tsort)
# p ObjectSpace.memsize_of(L.reverse!)

hoge = LL.each_cons(2).all? do |x,y|
    result[x].include?(y)
  end

puts LL
case hoge
when true  then puts 0
when false then puts 1
end