import scala.io.Source.stdin

object Main {
  def switchingRailroadCars(t: Iterator[String]): Vector[Int] = {
    def loop(n: Int,
      switchList: Vector[Int] = Vector(),
      exitList: Vector[Int] = Vector()): Vector[Int] = {
      if (t.nonEmpty) {
        n match {
          case 0 => loop(t.next.toInt, switchList.init, exitList :+ switchList.last)
          case _ => loop(t.next.toInt, switchList :+ n, exitList)
        }
      } else exitList :+ switchList.last
    }

    loop(t.next.toInt)
  }

  def main(args: Array[String]): Unit = {
    val trains = stdin.getLines
    switchingRailroadCars(trains).foreach(println)
  }
}