n = int(input()) alst = list(map(int, input().split())) one = 0 vw = [] tot = 0 cnt = 0 for a in alst: if a == 2: if tot != 0: vw.append((cnt, tot)) cnt = tot = 0 elif a == 0: tot += 1 else: cnt += 1 tot += 1 one += 1 if tot != 0: vw.append((cnt, tot)) dp = [-10 ** 20 for _ in range(one + 1)] dp[0] = 0 for v, w in vw: for j in range(one, w - 1, -1): dp[j] = max(dp[j], dp[j - w] + v) if dp[one] < 0: print(-1) else: print(one - dp[-1])