n=int(input())
l=[int(x) for x in input().split()]
w=min(l)-2
if w<0:
    w+=4
m=w%2
moves=0
h=1
for i in range(len(l)):
    x=l[i]
    if x%2!=m:
        if x-l[i-1]>1:
            w=x-1
            h=-1
        elif  (i<len(l)-1 and l[i+1]-x>1):
            w=x+1
            h=1
        d=(x-w) if x>w else (w-x)
        moves+=d
        w+=2*h
    elif x>1:
        if w==x:
            w+=2*h
        if w>x:
            d=(x-w) if x>w else (w-x)
            moves+=d
            w+=2*h
print(moves)
    
        
