object Main extends App {
  var n = scala.io.StdIn.readInt()
  var s = scala.io.StdIn.readLine()
  val inputArray =  s.split(" ").map(_.toInt)
  var i = 0

  def judgeWater(s: Seq[Int]): Seq[Int] = {
    var returnSeq = Seq.empty[Int]
    var stopFlg = false
    for(i<-0 to n - 1) {
      if (s(i) == 0 || stopFlg) {
        returnSeq :+= s(i)
      } else if (i == (s.size - 1) && !stopFlg) {
        returnSeq :+= s(i) - 1
      } else if (s(i + 1) == 0 && !stopFlg) {
        stopFlg = true
        returnSeq :+= s(i) - 1
      } else {
        returnSeq :+= s(i) - 1
      }
    }
    returnSeq
  }
  def judgeSeq(seqH: Seq[Int]): Unit = {
    i += 1
    val seqAfterWater = judgeWater(seqH)
    if (seqAfterWater.filterNot(_ == 0).nonEmpty) {
      judgeSeq(seqAfterWater)
    } else {
      println(i)
    }
  }
  judgeSeq(inputArray)
}