n = int(input())
*a, = map(int,input().split())
a.append(2)
res = []
c = d = 0
for ai in a:
    if ai == 0:
        c += 1
    elif ai == 1:
        d += 1
    else:
        if c or d:
            res.append((c,d))
        c = d = 0
dp = {0:0} # 差が k になる最小回数が v
from collections import defaultdict
for c,d in res:
    ndp = defaultdict(lambda :100000)
    for k,v in dp.items():
        ndp[k+c] = min(ndp[k+c],v+c)
        ndp[k-d] = min(ndp[k-d],v+d)
    dp = ndp
if 0 in dp:
    print(dp[0]//2)
else:
    print(-1)