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(); ArrayList meat = new ArrayList<>(); ArrayList bege = new ArrayList<>(); int total = 0; int count = 0; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (x == 2 && total > 0) { meat.add(count); bege.add(total - count); } if (x == 2) { count = 0; total = 0; continue; } total++; count += x; } if (total > 0 && count > 0 && count < total) { meat.add(count); bege.add(total - count); } int length = meat.size(); ArrayList> dp = new ArrayList<>(); for (int i = 0; i <= length; i++) { dp.add(new HashMap<>()); } dp.get(0).put(0, 0); for (int i = 1; i <= length; i++) { int m = meat.get(i - 1); int b = bege.get(i - 1); for (int x : dp.get(i - 1).keySet()) { dp.get(i).put(x + m, Math.min(dp.get(i).getOrDefault(x + m, Integer.MAX_VALUE), dp.get(i - 1).get(x) + m)); dp.get(i).put(x - b, Math.min(dp.get(i).getOrDefault(x - b, Integer.MAX_VALUE), dp.get(i - 1).get(x))); } } System.out.println(dp.get(length).getOrDefault(0, -1)); } } 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 double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }