from sys import stdin n, *a = map(int, stdin.read().split()) niku = [] nonbird = [] nowniku = 0 nownonbird = 0 sumniku = 0 for i in range(n): if a[i] == 0: nowniku += 1 sumniku += 1 nownonbird += 1 elif a[i] == 1: nownonbird += 1 else: if nownonbird != 0: niku.append(nowniku) nonbird.append(nownonbird) nowniku = 0 nownonbird = 0 if nownonbird != 0: niku.append(nowniku) nonbird.append(nownonbird) m = len(niku) dp = [[n+1 for i in range(sumniku+1)] for j in range(m+1)] dp[0][0] = 0 for i in range(m): for j in range(sumniku+1): dp[i+1][j] = min(dp[i+1][j],dp[i][j] + niku[i]) if j >= nonbird[i]: dp[i+1][j] = min(dp[i+1][j],dp[i][j-nonbird[i]] + nonbird[i] - niku[i]) if dp[m][sumniku] == n+1: print("{}".format(-1)) else: print("{}".format(dp[m][sumniku]//2))