
object Main extends App {
  import scala.io.StdIn._
  val n = readLine().toInt
  val nums = (0 until n).map(_ => readLine().toInt).toArray
  if (withNext(nums, -1).forall{case (prev, now) => (now - prev) < 2}){
    println(withNext(nums, 0).map{case (prev, now) => if (now - prev == 1) 1 else now}.sum)
  }else{
    println(-1)
  }
  def withNext[T](array:Array[T], first:T):Array[(T, T)] = {
    return array.zipWithIndex.map{
      case (factor, 0) => (first, factor)
      case (factor, idx) => (array(idx - 1), factor)
    }
  }

}
