n=gets.to_i
hNumbers=gets.split.map(&:to_i)
hNumberLength = hNumbers.length
tempHNumbers=Array.new(hNumberLength,0)
loopCount=0
loop do
  completNumber=0
  growFlag=false
  hNumbers.each_with_index do |value,index|
    if tempHNumbers[index] < value
      tempHNumbers[index] += 1 
      growFlag = true
    elsif tempHNumbers[index] == value
      break if growFlag
      completNumber += 1
    elsif tempHNumbers[index] > value && growFlag
      break
    end
  end
  loopCount += 1
  break if hNumberLength == completNumber
end
puts (loopCount-1)