N = int(eval(input()))

cnt = 0

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

if A[0]!=0:

    cnt = -1

else:

    for i in range(1,N):

        if A[i]==A[i-1]+1:

            continue

        elif A[i]<=A[i-1]:

            cnt += A[i-1]

        else:

            cnt = -1

            break

    if cnt>=0:

        cnt += A[N-1]

print(cnt)  
N = int(eval(input()))

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

cnt = A[-1]

flag = 0

for i in range(N-2,-1,-1):

    if A[i]==A[i+1] or A[i]>A[i+1]:

        cnt += A[i]

    elif A[i] == A[i+1]-1:

        continue

    else:

        flag = 1

        break

if A[0]!=0:

    flag = 1

if flag==0:

    print(cnt)

else:

    print((-1))
N = int(eval(input()))
cnt = 0
A = [int(eval(input())) for _ in range(N)]
if A[0] != 0:
    cnt = -1
else:
    for i in range(1, N):
        if A[i] == A[i - 1] + 1:
            continue
        elif A[i] <= A[i - 1]:
            cnt += A[i - 1]
        else:
            cnt = -1
            break
    if cnt >= 0:
        cnt += A[N - 1]
print(cnt)

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
cnt = A[-1]
flag = 0
for i in range(N - 2, -1, -1):
    if A[i] == A[i + 1] or A[i] > A[i + 1]:
        cnt += A[i]
    elif A[i] == A[i + 1] - 1:
        continue
    else:
        flag = 1
        break
if A[0] != 0:
    flag = 1
if flag == 0:
    print(cnt)
else:
    print((-1))

-cnt = 0 +cnt = A[-1] +flag = 0 +for i in range(N - 2, -1, -1): +    if A[i] == A[i + 1] or A[i] > A[i + 1]: +        cnt += A[i] +    elif A[i] == A[i + 1] - 1: +        continue +    else: +        flag = 1 +        break -    cnt = -1 +    flag = 1 +if flag == 0: +    print(cnt) -    for i in range(1, N): -        if A[i] == A[i - 1] + 1: -            continue -        elif A[i] <= A[i - 1]: -            cnt += A[i - 1] -        else: -            cnt = -1 -            break -    if cnt >= 0: -        cnt += A[N - 1] -print(cnt) +    print((-1))