import scala.collection.mutable

object Main extends App {
  import scala.io.StdIn.{readLine}
  class Team(val id:Int){
    private var loseBy:Int = 0
    private var mWinTo:List[Team] = Nil
    def decreaseLoseCount():Unit = loseBy-=1
    def loseCount:Int = loseBy
    def winTo:List[Team] = mWinTo
  }
  object Team{
    def setMatchResult(winner:Team, loser:Team):Unit = {
      winner.mWinTo = loser::winner.mWinTo
      loser.loseBy += 1
    }
  }
  val team = Array.tabulate(readLine().toInt){i ⇒ new Team(i + 1)}
  for (_ ← 0 until readLine().toInt){
    val Array(winner, loser) = readLine().split(' ').map(_.toInt)
    Team.setMatchResult(team(winner - 1), team(loser - 1))
  }
  val queue:mutable.Queue[Team] = new mutable.Queue[Team]
  queue.enqueue(team.filter(_.loseCount == 0):_*)
  var hasSomeSuspect = false
  while (queue.size != 0){
    hasSomeSuspect ||= queue.size != 1
    val top = queue.dequeue()
    queue.enqueue(top.winTo.filter { case t ⇒ t.decreaseLoseCount()
        t.loseCount == 0
    }:_*)
    println(top.id)
  }
  println(if (hasSomeSuspect) 1 else 0)
}
