n = int(eval(input()))

a = [int(eval(input())) for _i in range(n)] + [-1]



result = -1

for i in range(n):

    if a[i-1] >= a[i]:

        result += a[i]

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

        result += 1

    else:

        result = -1

        break

print(result)
n,r=int(eval(input())),-1

a = [int(eval(input())) for _i in range(n)]+[-1]

for i in range(n):

    if a[i-1] >= a[i]:r+=a[i]

    elif a[i-1]+1==a[i]:r+=1

    else:r=-1;break

print(r)
n = int(eval(input()))
a = [int(eval(input())) for _i in range(n)] + [-1]
result = -1
for i in range(n):
    if a[i - 1] >= a[i]:
        result += a[i]
    elif a[i - 1] + 1 == a[i]:
        result += 1
    else:
        result = -1
        break
print(result)

n, r = int(eval(input())), -1
a = [int(eval(input())) for _i in range(n)] + [-1]
for i in range(n):
    if a[i - 1] >= a[i]:
        r += a[i]
    elif a[i - 1] + 1 == a[i]:
        r += 1
    else:
        r = -1
        break
print(r)

-n = int(eval(input())) +n, r = int(eval(input())), -1 -result = -1 -        result += a[i] +        r += a[i] -        result += 1 +        r += 1 -        result = -1 +        r = -1 -print(result) +print(r)