import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int zeroCount = 0; int oneCount = 0; ArrayList zeros = new ArrayList<>(); ArrayList ones = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (x == 2) { if (zeroCount > 0 || oneCount > 0) { zeros.add(zeroCount); ones.add(oneCount); zeroCount = 0; oneCount = 0; } } else if (x == 0) { zeroCount++; } else { oneCount++; } } if (zeroCount > 0 || oneCount > 0) { zeros.add(zeroCount); ones.add(oneCount); } int total = 0; int size = zeros.size(); for (int x : zeros) { total += x; } int[] dp = new int[total + 1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int i = 0; i < size; i++) { int sum = zeros.get(i) + ones.get(i); int value = ones.get(i); for (int j = total; j - sum >= 0; j--) { if (dp[j - sum] == Integer.MAX_VALUE) { continue; } dp[j] = Math.min(dp[j], dp[j - sum] + value); } } if (dp[total] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(dp[total]); } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }