n = int(eval(input()))

A = [int(eval(input()))for _ in range(n)]

if A[0] != 0:

    print((-1))

    exit()



ans = A[-1]

for a, prev_a in reversed(tuple(zip(A, A[1:]))):

    if a == prev_a-1:

        continue

    if a < prev_a-1:

        print((-1))

        break

    ans += a

else:

    print(ans)

n = int(eval(input()))

ans = -1

prev = -1

for _ in range(n):

    a = int(eval(input()))

    if prev+1 == a:

        prev = a

        ans += 1

        continue

    if prev+1 < a:

        print((-1))

        break

    ans += a

    prev = a

else:

    print(ans)

n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
if A[0] != 0:
    print((-1))
    exit()
ans = A[-1]
for a, prev_a in reversed(tuple(zip(A, A[1:]))):
    if a == prev_a - 1:
        continue
    if a < prev_a - 1:
        print((-1))
        break
    ans += a
else:
    print(ans)

n = int(eval(input()))
ans = -1
prev = -1
for _ in range(n):
    a = int(eval(input()))
    if prev + 1 == a:
        prev = a
        ans += 1
        continue
    if prev + 1 < a:
        print((-1))
        break
    ans += a
    prev = a
else:
    print(ans)

-A = [int(eval(input())) for _ in range(n)] -if A[0] != 0: -    print((-1)) -    exit() -ans = A[-1] -for a, prev_a in reversed(tuple(zip(A, A[1:]))): -    if a == prev_a - 1: +ans = -1 +prev = -1 +for _ in range(n): +    a = int(eval(input())) +    if prev + 1 == a: +        prev = a +        ans += 1 -    if a < prev_a - 1: +    if prev + 1 < a: +    prev = a