mod = 1000000007 eps = 10**-9 inf = 10**15 def main(): import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) A.append(2) B = [] cnt = [0, 0] for a in A: if a == 2: if sum(cnt): B.append(cnt) cnt = [0, 0] else: cnt[a] += 1 S = 0 for a, b in B: S += b dp = [inf] * (S+1) dp[S] = 0 for i in range(len(B)): n0, n1 = B[i] dp_new = [inf] * (S+1) for j in range(S+1): dp_new[j] = min(dp_new[j], dp[j]) if j - (n0 + n1) >= 0: dp_new[j - (n0 + n1)] = min(dp_new[j - (n0 + n1)], dp[j] + n0) dp = dp_new if dp[0] != inf: print(dp[0]) else: print(-1) if __name__ == '__main__': main()