n = int(input()) a = list(map(int, input().split())) a.append(2) b = [] c0 = c1 = 0 for x in a: if x == 0: c0 += 1 elif x == 1: c1 += 1 else: if c0 or c1: b.append((c0, c1)) c0 = c1 = 0 INF = 10 ** 9 dp = [INF] * (2 * n + 10) B = n + 5 dp[B] = 0 for c0, c1 in b: ndp = [INF] * (2 * n + 10) for i in range(-n, n): if dp[B + i] == INF: continue ndp[B + i + c1] = min(ndp[B + i + c1], dp[B + i] + c1) ndp[B + i - c0] = min(ndp[B + i - c0], dp[B + i] + c0) dp = ndp ans = ndp[B] if ans == INF: ans = -1 else: ans //= 2 print(ans)