// 提出時はMainに変更
object Main extends App {
  val (us, limits) = {
    val s = new java.util.Scanner(System.in)
    (s.nextLong(), (1 to 5).map(_ => s.nextLong()))
  }

  def moveStep(current: Long, movable: Long): (Long, Long) =
    current match {
      case 0 => (0, 0)
      case c if c > movable => (c - movable, movable)
      case c => (0, c)
    }

  def move(before: Seq[Long]): Seq[Long] = {
    val after = (0 to 4).map(i => moveStep(before(i), limits(i)))
    Seq(
      after(0)._1,
      after(0)._2 + after(1)._1,
      after(1)._2 + after(2)._1,
      after(2)._2 + after(3)._1,
      after(3)._2 + after(4)._1,
      after(4)._2
    )
  }

  @scala.annotation.tailrec
  def calc(before: Seq[Long], count: Long = 0): Long = {
    before match {
      case Seq(0, 0, 0, 0, 0, _) => count
      case b => calc(move(b), count + 1)
    }
  }

  val init = Seq(us, 0, 0, 0, 0, 0)
  println(calc(init))
}
