n=int(eval(input()))

lists=[int(eval(input()))for i in range(n)]

lists.append(0)

ans=0

if lists[0]!=0:

    print((-1))

else:

    flag=True

    for i in range(n-1):

        if lists[i]+1<lists[i+1]:

            flag=False

            break

    if not flag:

        print((-1))

    else:

        for i in range(n):

            if lists[i]+1==lists[i+1]:

                ans+=1

            else:

                ans+=lists[i+1]

        print(ans)

        

        
#sequence growing easy

N=int(eval(input()))

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

answer=0

now_value=10**9

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

    if a[i]>i:

        answer=-1

        break 

    if a[i]==now_value-1:

        now_value-=1

    else:

        answer+=a[i]

        now_value=a[i]

for i in range(N-1):

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

         answer=-1

         break

print(answer)
n = int(eval(input()))
lists = [int(eval(input())) for i in range(n)]
lists.append(0)
ans = 0
if lists[0] != 0:
    print((-1))
else:
    flag = True
    for i in range(n - 1):
        if lists[i] + 1 < lists[i + 1]:
            flag = False
            break
    if not flag:
        print((-1))
    else:
        for i in range(n):
            if lists[i] + 1 == lists[i + 1]:
                ans += 1
            else:
                ans += lists[i + 1]
        print(ans)

# sequence growing easy
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
answer = 0
now_value = 10**9
for i in range(N - 1, -1, -1):
    if a[i] > i:
        answer = -1
        break
    if a[i] == now_value - 1:
        now_value -= 1
    else:
        answer += a[i]
        now_value = a[i]
for i in range(N - 1):
    if a[i + 1] - a[i] > 1:
        answer = -1
        break
print(answer)

-n = int(eval(input())) -lists = [int(eval(input())) for i in range(n)] -lists.append(0) -ans = 0 -if lists[0] != 0: -    print((-1)) -else: -    flag = True -    for i in range(n - 1): -        if lists[i] + 1 < lists[i + 1]: -            flag = False -            break -    if not flag: -        print((-1)) +# sequence growing easy +N = int(eval(input())) +a = [int(eval(input())) for _ in range(N)] +answer = 0 +now_value = 10**9 +for i in range(N - 1, -1, -1): +    if a[i] > i: +        answer = -1 +        break +    if a[i] == now_value - 1: +        now_value -= 1 -        for i in range(n): -            if lists[i] + 1 == lists[i + 1]: -                ans += 1 -            else: -                ans += lists[i + 1] -        print(ans) +        answer += a[i] +        now_value = a[i] +for i in range(N - 1): +    if a[i + 1] - a[i] > 1: +        answer = -1 +        break +print(answer)