let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d\n" @@ fun a -> a in
  if as_.(0) <> 0
  then print_endline "-1"
  else begin
    Array.init (n - 1) (( + ) 1)
    |> Array.map (fun i ->
        if as_.(i) = as_.(i - 1) + 1 then 1
        else as_.(i))
    |> Array.fold_left ( + ) 0
    |> Printf.printf "%d\n"
  end