import java.util.*

class Node(val Id:Int){
    private val mEdgesTo: LinkedList<Node> = LinkedList()
    var HasLoop:Boolean? = null
    fun AddEdge(to:Node):Unit = mEdgesTo.push(to)
    fun Edges():Iterable<Node> = mEdgesTo
}
fun findLoop(current:Node, count:Int = 0):Boolean{
    when(current.HasLoop){
        null -> when {
            count > 100 -> current.HasLoop = true
            else -> current.HasLoop = current.Edges().any{findLoop(it, count + 1)}
        }
    }
    return current.HasLoop!!
}
fun main(args:Array<String>):Unit{
    val (v, e) = readLine()!!.split(' ').map(String::toInt)
    val nodes = Array(v){Node(it)}
    for (i in 0 until e){
        val (s, t) = readLine()!!.split(' ').map(String::toInt)
        nodes[s].AddEdge(nodes[t])
    }
    println(if (nodes.any{findLoop(it)}) 1 else 0)
}
