Snuke stands on a number line with L ears and walks continuously under specific conditions. He puts a stone in his i-th ear each time he passes a point with coordinate i-0.5. After Snuke finishes walking, Ringo repeats operations to ensure each ear contains a specific number of stones. The task is to find the minimum number of operations required for Ringo to freely decide how Snuke walks. Constraints: 1 ≤ L ≤ 2×10^5, 0 ≤ A_i ≤ 10^9 (1 ≤ i ≤ L). Input format: L followed by A_1 to A_L. Output the minimum number of operations required.

# Buggy version:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = [0]*n
for i in range(n):
  if a[i]%2:
    b[i] = 1
  elif a[i]:
    b[i] = 2
n += 1
DP = [[10**18 for i in range(5)] for j in range(n+1)]
for i in range(5):
    DP[0][i] = 0
for i in range(1,n+1):
  for j in range(5):
    for k in range(j,5):
      if k == 0 or k == 4:
        DP[i][k] = min(DP[i][k],DP[i-1][j]+a[i-1])
      elif k == 1 or k == 3:
        DP[i][k] = min(DP[i][k],DP[i-1][j]+abs(b[i-1]-1))
      else:
        DP[i][k] = min(DP[i][k],DP[i-1][j]+abs(b[i-1]-2))
print(min(DP[n]))

# Fixed version:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = [0]*n
for i in range(n):
  if a[i]%2:
    b[i] = 1
  elif a[i]:
    b[i] = 2
DP = [[10**18 for i in range(5)] for j in range(n+1)]
for i in range(5):
    DP[0][i] = 0
for i in range(1,n+1):
  for j in range(5):
    for k in range(j,5):
      if k == 0 or k == 4:
        DP[i][k] = min(DP[i][k],DP[i-1][j]+a[i-1])
      elif k == 1 or k == 3:
        DP[i][k] = min(DP[i][k],DP[i-1][j]+abs(b[i-1]-2))
      else:
        DP[i][k] = min(DP[i][k],DP[i-1][j]+abs(b[i-1]-1))
print(min(DP[n]))

# Buggy version:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
dp = [[1<<60]*5 for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
        dp[i+1][0] = dp[i][0]+a[i]
        dp[i+1][1] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2)
        if a[i] == 0:
                pass
        else:
                a[i]+=1
        dp[i+1][2] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2,dp[i][2]+a[i]%2)
        if a[i] == 0:
                pass
        else:
                a[i]-=1
        dp[i+1][3] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2,dp[i][2]+a[i]%2,dp[i][3]+a[i]%2)
        dp[i+1][4] = min(dp[i][0],dp[i][1],dp[i][2],dp[i][3],dp[i][4])+a[i]
print(min(dp[n]))

# Fixed version:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
dp = [[1<<60]*5 for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
        dp[i+1][0] = dp[i][0]+a[i]
        if a[i] != 0:
                dp[i+1][1] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2)
        else:
                dp[i+1][1] = min(dp[i][0]+2,dp[i][1]+2)
        a[i]+=1
        dp[i+1][2] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2,dp[i][2]+a[i]%2)
        a[i]-=1
        if a[i] != 0:
                dp[i+1][3] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2,dp[i][2]+a[i]%2,dp[i][3]+a[i]%2)
        else:
                dp[i+1][3] = min(dp[i][0]+2,dp[i][1]+2,dp[i][2]+2,dp[i][3]+2)
        dp[i+1][4] = min(dp[i][0],dp[i][1],dp[i][2],dp[i][3],dp[i][4])+a[i]
print(min(dp[n]))

# Buggy version:
a = int(input())

b = []

for i in range(a):
	b.append(int(input()))

c = [[0] * 5 for i in range(a+1)]

for i in range(a):
	c[i+1][0] = c[i][0] + b[i]
	c[i+1][1] = min(c[i][0],c[i][1]) + (b[i] % 2 if b[i] > 0 else 2)
	c[i+1][2] = min(c[i][0][,c[i][1],c[i][2]]) + ((b[i]+1) % 2)
	c[i+1][3] = min(c[i][0][,c[i][1],c[i][2]],c[i][3]) + (b[i] % 2 if b[i] > 0 else 2)
	c[i+1][4] = min(c[i][0][,c[i][1],c[i][2]],c[i][3],c[i][4]) + b[i]

print(min(c[-1]))

# Fixed version:
a = int(input())

b = []

for i in range(a):
	b.append(int(input()))

c = [[0] * 5 for i in range(a+1)]

for i in range(a):
	c[i+1][0] = c[i][0] + b[i]
	c[i+1][1] = min(c[i][0],c[i][1]) + (b[i] % 2 if b[i] > 0 else 2)
	c[i+1][2] = min(c[i][0],c[i][1],c[i][2]) + ((b[i]+1) % 2)
	c[i+1][3] = min(c[i][0],c[i][1],c[i][2],c[i][3]) + (b[i] % 2 if b[i] > 0 else 2)
	c[i+1][4] = min(c[i][0],c[i][1],c[i][2],c[i][3],c[i][4]) + b[i]

print(min(c[-1]))

# Buggy version:
l = int(input())
a = [int(input()) for _ in range(l)]

dp = [[0 for i in range(5)] for j in range(l+1)]

def cost(s, a):
    if s == 0 or s == 4:
        return a
    elif s == 1 or s == 3:
        if a > 0:
            return a%2
        else:
            return 0
    else:
        return (a+1)%2

for i in range(l):
    dp[i+1][0] = min(dp[i][:1]) + cost(0, a[i])
    dp[i+1][1] = min(dp[i][:2]) + cost(1, a[i])
    dp[i+1][2] = min(dp[i][:3]) + cost(2, a[i])
    dp[i+1][3] = min(dp[i][:4]) + cost(3, a[i])
    dp[i+1][4] = min(dp[i][:5]) + cost(4, a[i])

print(min(dp[l]))



# Fixed version:
l = int(input())
a = [int(input()) for _ in range(l)]

dp = [[0 for i in range(5)] for j in range(l+1)]

def cost(s, a):
    if s == 0 or s == 4:
        return a
    elif s == 1 or s == 3:
        if a > 0:
            return a%2
        else:
            return 2
    else:
        return (a+1)%2

for i in range(l):
    dp[i+1][0] = min(dp[i][:1]) + cost(0, a[i])
    dp[i+1][1] = min(dp[i][:2]) + cost(1, a[i])
    dp[i+1][2] = min(dp[i][:3]) + cost(2, a[i])
    dp[i+1][3] = min(dp[i][:4]) + cost(3, a[i])
    dp[i+1][4] = min(dp[i][:5]) + cost(4, a[i])

print(min(dp[l]))

# Buggy version:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)v

# Fixed version:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

# Buggy version:
#https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_d
#2019-02-11

#DP
L = int(input())
DP = [0 for i in range(5)]

def cost(a):
    if a:
        return [a, a%2, (a+1)%2, a%2, a]
    else:
        return [0, 2, 1, 2, 0]

for i in range(L):
    n = [min(DP[:i+1]) for i in range(5)]
    cos = cost(a)
    DP[4] = n[4] + cos[4]
    DP[3] = n[3] + cos[3]
    DP[2] = n[2] + cos[2]
    DP[1] = n[1] + cos[1]
    DP[0] = n[0] + cos[0]

print(min(DP))

# Fixed version:
#https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_d
#2019-02-11

#DP

L = int(input())

DP = [0 for i in range(5)]

def cost(a):
    if a:
        return [a, a%2, (a+1)%2, a%2, a]
    else:
        return [0, 2, 1, 2, 0]

for i in range(L):
    n = [min(DP[:i+1]) for i in range(5)]
    a = int(input())
    cos = cost(a)
    DP[0] = n[0] + cos[0]
    DP[1] = n[1] + cos[1]
    DP[2] = n[2] + cos[2]
    DP[3] = n[3] + cos[3]
    DP[4] = n[4] + cos[4]



print(min(DP))

# Buggy version:
# https://yahoo-procon2019-qual.contest.atcoder.jp/tasks/yahoo_procon2019_qual_d

L = int(input())

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

INF = int(1e15)
dp = [[INF] * (L + 1)] for _ in range(5)]

for i in range(4):
    dp[i][0] = 0

for i in range(n):
    dp[0][i + 1] = dp[0][i] + A[i]  # 全部取り除く
    dp[1][i + 1] = min(dp[j][i] for j in range(2)) + (A[i] % 2 if A[i] != 0 else 2)
    dp[2][i + 1] = min(dp[j][i] for j in range(3)) + (1 if A[i] % 2 == 0 else 0)
    dp[3][i + 1] = min(dp[j][i] for j in range(4)) + (A[i] % 2 if A[i] != 0 else 2)
    dp[4][i + 1] = min(dp[j][i] for j in range(5)) + A[i]  # 全部取り除く


print(min(dp[i][L] for i in range(1, 5)))


# Fixed version:
# https://yahoo-procon2019-qual.contest.atcoder.jp/tasks/yahoo_procon2019_qual_d

L = int(input())

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

INF = int(1e15)
dp = [[INF] * (L + 1) for _ in range(5)]

for i in range(4):
    dp[i][0] = 0

for i in range(L):
    dp[0][i + 1] = dp[0][i] + A[i]  # 全部取り除く
    dp[1][i + 1] = min(dp[j][i] for j in range(2)) + (A[i] % 2 if A[i] != 0 else 2)
    dp[2][i + 1] = min(dp[j][i] for j in range(3)) + (1 if A[i] % 2 == 0 else 0)
    dp[3][i + 1] = min(dp[j][i] for j in range(4)) + (A[i] % 2 if A[i] != 0 else 2)
    dp[4][i + 1] = min(dp[j][i] for j in range(5)) + A[i]  # 全部取り除く


print(min(dp[i][L] for i in range(1, 5)))

# Buggy version:
#https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_d
#2019-02-10

#DP

L = int(input())
A = [int(input()) for i in range(L)]

DP = [[float("inf") for i in range(5)] for j in range(L)]
DP[0][0] = 0

def cost(s, i, A):
    if s == 0 or s == 4:
        return A[i]
    elif s == 1 or s == 3:
        if A[i] > 0:
            if A[i]%2 == 0:
                return 0
            else:
                return 1
        else:
            return 2
    else:
        if A[i]%2 == 1:
            return 1
        else:
            return 0

for i in range(L-1):
    for j in range(5):
        for k in range(j,5):
            DP[i+1][k] = min(DP[i+1][k], DP[i][j]+ cost(k, i, A))



print(min(DP[L-1]))

# Fixed version:
#https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_d
#2019-02-10

#DP

L = int(input())
A = [int(input()) for i in range(L)]

DP = [[float("inf") for i in range(5)] for j in range(L+1)]
for i in range(5):
    DP[0][i] = 0

def cost(s, i, A):
    if s == 0 or s == 4:
        return A[i]
    elif s == 1 or s == 3:
        if A[i] > 0:
            if A[i]%2 == 0:
                return 0
            else:
                return 1
        else:
            return 2
    else:
        if A[i]%2 == 1:
            return 0
        else:
            return 1

for i in range(L):
    for j in range(5):
        for k in range(j,5):
            DP[i+1][k] = min(DP[i+1][k], DP[i][j]+ cost(k, i, A))



print(min(DP[L]))

# Buggy version:
l = int(input())
a = []
for i in range(l):
  a.append(int(input()))
  
dp=[[0]*5 for i in range(l+1)]

for i in range(0,l):
  
  dp[i+1][0]=dp[i][0]+a[i]
  dp[i+1][1]=min(dp[i][0],dp[i][1]) + a[i]%2
  dp[i+1][2]=min(dp[i][0],dp[i][1],dp[i][2]) + (a[i]+1)%2
  dp[i+1][3]=min(dp[i][0],dp[i][1],dp[i][2],dp[i][3]) + a[i]%2
  dp[i+1][4]=min(dp[i][0],dp[i][1],dp[i][2],dp[i][3],dp[i][4]) + a[i]
  
print(min(dp[l]))

# Fixed version:
l = int(input())
a = []
for i in range(l):
  a.append(int(input()))
  
dp=[[0]*5 for i in range(l+1)]

for i in range(0,l):
  if a[i]==0:
    even=2
  else:
    even=a[i]%2
  
  dp[i+1][0]=dp[i][0]+a[i]
  dp[i+1][1]=min(dp[i][0],dp[i][1]) + even
  dp[i+1][2]=min(dp[i][0],dp[i][1],dp[i][2]) + (a[i]+1)%2
  dp[i+1][3]=min(dp[i][0],dp[i][1],dp[i][2],dp[i][3]) + even
  dp[i+1][4]=min(dp[i][0],dp[i][1],dp[i][2],dp[i][3],dp[i][4]) + a[i]
  
print(min(dp[l]))

# Buggy version:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
L = int(input())
A = [int(input()) for _ in  range(L)]


DP = [0] * 5
for i in range(L):
    r0 = A[i]
    r1 = (A[i] + 1) % 2

    if A[i] == 0:
        r2 = 2
    else:
        r2 = A[i] % 2

    for i,r in zip(range(4,-1,-1), [r0,r2,r1,r2,r0]):
        DP[i] = min(DP[:i+1]) + r
        print(DP)
print(min(DP))
    


# Fixed version:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
L = int(input())
A = [int(input()) for _ in  range(L)]


DP = [0] * 5
for i in range(L):
    r0 = A[i]
    r1 = (A[i] + 1) % 2

    if A[i] == 0:
        r2 = 2
    else:
        r2 = A[i] % 2

    for i,r in zip(range(4,-1,-1), [r0,r2,r1,r2,r0]):
        DP[i] = min(DP[:i+1]) + r
        # print(DP)

print(min(DP))

# Buggy version:
a = [int(input()) for _ in range(int(input()))]

dp = [[0] * 5 for _ in range(len(a) + 1)]

for i, x in enumerate(a):
    y = x % 2 + (x==0) * 2
    z = (x + 1) % 2
    
    dp[i+1][0] = dp[i][0] + x
    dp[i+1][1] = min(dp[i][0] + y, dp[i][1] + y)
    dp[i+1][2] = min(dp[i][1] + z, dp[i][0] + z, dp[i][2] + z)
    dp[i+1][3] = min(dp[i][2] + y, dp[i][1] + y, dp[i][0] + y, dp[i][3] + y)
    dp[i+1][4] = min(dp[i][3] + x, dp[i+1][3])

print(dp[-1][4])

# Fixed version:
a = [int(input()) for _ in range(int(input()))]

dp = [[0] * 5 for _ in range(len(a) + 1)]

for i, x in enumerate(a):
    y = x % 2 + (x == 0) * 2
    z = (x + 1) % 2
    
    dp[i+1][0] = min(dp[i][0:1]) + x
    dp[i+1][1] = min(dp[i][0:2]) + y
    dp[i+1][2] = min(dp[i][0:3]) + z
    dp[i+1][3] = min(dp[i][0:4]) + y
    dp[i+1][4] = min(dp[i][0:5]) + x

print(min(dp[-1]))

# Buggy version:
L = int(input())

inf = float('inf')
dp = [[inf] * (L+1) for _ in range(5)]
dp[0][0] = 0
for i in range(1, L+1):
    a = int(input())
    if a == 0:
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + 2
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + 1
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + 2
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1])
    else:
        b = a & 1
        c = b ^ 1
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + b
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + c
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + b
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1]) + a

print(min(dp[3][L], dp[4][L]))

# Fixed version:
L = int(input())

inf = float('inf')
dp = [[inf] * 5 for _ in range(L+1)]
dp[0][0] = 0
for i in range(1, L+1):
    a = int(input())

    b = a & 1
    c = b ^ 1
    p, n = dp[i-1], dp[i]
    n[0] = p[0] + a
    n[1] = min(p[:2]) + b
    n[2] = min(p[:3]) + c
    n[3] = min(p[:4]) + b
    n[4] = min(p[:5]) + a

    if a == 0:
        n[1] += 2
        n[3] += 2

print(min(dp[L]))

# Buggy version:
L = int(input())
A = [0] * (L + 1)
DP = [[0, 0, 0, 0, 0] for i in range(L + 1)]
for i in range(L):
  a = int(input())
  DP[i + 1][0] = DP[i][0] + a
  DP[i + 1][1] = min(DP[i][1], DP[i + 1][0]) + a % 2
  DP[i + 1][2] = min(min(DP[i][:3]), DP[i + 1][1]) + (1 - a % 2)
  DP[i + 1][3] = min(min(DP[i][:4]), DP[i + 1][2]) + a % 2
  DP[i + 1][4] = min(min(DP[i]), DP[i + 1][3]) + a
print(min(DP[L]))

# Fixed version:
L = int(input())
A = [0] * (L + 1)
DP = [[0, 0, 0, 0, 0] for i in range(L + 1)]
for i in range(L):
  a = int(input())
  DP[i + 1][0] = DP[i][0] + a
  DP[i + 1][1] = min(min(DP[i][:2]), DP[i + 1][0]) + (a % 2 if a else 2)
  DP[i + 1][2] = min(min(DP[i][:3]), DP[i + 1][1]) + ((1 - a % 2) if a else 1)
  DP[i + 1][3] = min(min(DP[i][:4]), DP[i + 1][2]) + (a % 2 if a else 2)
  DP[i + 1][4] = min(min(DP[i]), DP[i + 1][3]) + a
print(min(DP[L]))