let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let a = Array.to_list @@ Array.init n @@ fun _ -> int_of_string @@ read_line ()

let () =
  if List.hd a != 0 then print_endline "-1"
  else
    let (sum, _) = List.fold_left (fun (sum, before) i ->
      if sum = -1 then (-1, i)
      else let t = i - before in
        if t > 1 then (-1,  i)
        else if t = 1 then (sum + 1, i)
        else (sum + i, i)
      ) (0, 0) @@ List.tl a in
    Printf.printf "%d\n" sum