import java.util.Scanner

import scala.collection.mutable.ArrayBuffer

object Main {
  def main(args: Array[String]): Unit = println(solve(new Scanner(System.in)))

  def solve(in: String): String = solve(new Scanner(in))

  def solve(sc: Scanner): String = {
    val N = sc.nextInt
    val A = Array.fill(N)(sc.nextInt)
    val B = ArrayBuffer.fill(N)(0)

    var ans = 0
    for (i <- (N - 1) to 0 by -1) {
      if (A(i) > i) {
        return "-1"
      }
      for (j <- 0 until A(i)) {
        val n = A(i) - j
        if (B(i - j) < n) {
          B(i - j) = n
          ans += 1
        } else if (B(i - j) > n) {
          return "-1"
        }

      }
    }
    ans.toString
  }
}
