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
  var seqVar = Seq.empty[Int]
  for (i<-0 to n - 1) {
    seqVar :+= 0
  }

  def addWater(s: Seq[Int]): Seq[Int] = {
    var returnSeq = Seq.empty[Int]
    var stopFlg = false
    for(i<-0 to n - 1) {
      if (s(i) == inputArray(i) || stopFlg) {
        returnSeq :+= s(i)
      } else if (i == (s.size - 1)) {
        returnSeq :+= s(i) + 1
      } else if (s(i + 1) == inputArray(i + 1)) {
        stopFlg = true
        returnSeq :+= s(i) + 1
      } else {
        returnSeq :+= s(i) + 1
      }
    }
    returnSeq
  }
  def compareSeq(s1: Seq[Int], s2: Seq[Int]): Boolean= {
    for(i<-0 to n - 1) {
      if(s1(i) != s2(i)) {
        return false
      }
    }
    true
  }

  def judgeSeq(s: Seq[Int]): Unit = {
    i += 1
    val seqAfterWater = addWater(s)
    if (compareSeq(inputArray, seqAfterWater)) {
      println(i)
    } else {
      judgeSeq(seqAfterWater)
    }
  }
  judgeSeq(seqVar)
}