
object Main extends App {

  import scala.io.StdIn._
  solve
  def solve: Unit = {
    val line = readLine.trim
    if (line == "0") return
    val nums = line.split(' ').map(_.toInt)
    if (solve(nums(1), nums.drop(2))) println(1)
    else println(0)
    solve
  }
  def solve(stone: Int, players: Array[Int]): Boolean = {
    val memo = Array.tabulate(stone + 1){_ ⇒ Array.tabulate(players.length + 1){_ ⇒ None: Option[Boolean]}}
    def canWin(restStone: Int, currentPlayer: Int): Boolean = {
      if (restStone - 1 <= players(currentPlayer)) currentPlayer % 2 == 0
      else {
        memo(restStone)(currentPlayer) match {
          case None if currentPlayer % 2 == 0 ⇒ (1 to players(currentPlayer)).reverse.exists(take ⇒ canWin(restStone - take, (currentPlayer + 1) % players.length)).also(r ⇒ memo(restStone)(currentPlayer) = Some(r))
          case None ⇒ (1 to players(currentPlayer)).reverse.forall(take ⇒ canWin(restStone - take, (currentPlayer + 1) % players.length)).also(r ⇒ memo(restStone)(currentPlayer) = Some(r))
          case Some(r) ⇒ r
        }
      }
    }
    for (r ← 0 until stone by 100) canWin(r, 0)
    canWin(stone, 0)
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
    @inline
    def takeIf(@inline predicate: T ⇒ Boolean): Option[T] = Some(value).filter(predicate)
  }
}
