class Node
  attr_accessor :child, :branch
  def initialize
    @child = []
    @branch = nil
  end
end

def dfs(g)
  done = {}
  v = g.size
  j = 0
  v.times{|i|
    next if g[i].branch != nil
    stack = []
    g[i].branch = j

    stack << i
    while stack.size > 0
      u = stack[-1]
      flag = true
      g[u].child.each{|x|
        next if done[[u,x]]
        if g[x].branch == nil
          g[x].branch = j
          done[[u, x]] = true
          stack << x
          flag = false
          break
        elsif g[x].branch == j
          #Loop detected
          puts 1
          exit
        end
      }
      if flag == true
        stack.pop
        j += 1
      end
    end
  }
end


v, e = gets.split.map(&:to_i)
g = Array.new(v){Node.new}

e.times{
  s, t = gets.split.map(&:to_i)
  g[s].child << t
}

dfs(g)

puts 0