n=int(input())
x=list(map(int, input().split()))
length=len(x)
count=0
sum1=0
sum2=0
for i in range(0,len(x)-1):
    if(x[i] == x[i+1]):
        count=0
    elif(x[i]>x[i+1]):
        diffrence=x[i]-x[i+1]
        sum1+=diffrence
        count+=1
    elif(x[i]<x[i+1]):
        diffrence=x[i]-x[i+1]
        sum2+=diffrence
        count-=1
if(count==0):
    print(0)

elif(count>1):
    print(sum1+1)
elif(count<1):
    print(sum2+1)
