# ??±????????????????????????????????????
L = [] # ???????????????????????????????´?
v = {} # ?¨???????????????????????????????(true????´?)
def visit(node, visited)
  unless visited[node]
    visited[node] = true
    $result[node].each do |x|
      visit(x, visited)
    end
    L << node
  end
end

#???????????°???????????°??\???
n_teams = gets.to_i
games   = gets.to_i

#???????????????hash???????????§??????
$result = {}
(1..n_teams).each{ |team| $result[team] = [] }
games.times do
  win,lose = gets.split.map(&:to_i)
  $result[win] << lose
end

# p ObjectSpace.memsize_of()
(1..n_teams).each do |team|
  visit(team, v)
end
L.reverse!

exist = L.each_cons(2).all? do |x,y|
    $result[x].include?(y)
  end

puts L
case exist
when true  then puts 0
when false then puts 1
end