import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static ArrayList meats = new ArrayList<>(); static ArrayList grasses = new ArrayList<>(); static ArrayList>> dp = new ArrayList<>(); public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int mCount = 0; int gCount = 0; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (x == 0) { mCount++; } else if (x == 1) { gCount++; } else { if (mCount > 0 || gCount > 0) { meats.add(mCount); grasses.add(gCount); dp.add(new HashMap<>()); mCount = 0; gCount = 0; } } } if (mCount > 0 || gCount > 0) { meats.add(mCount); grasses.add(gCount); dp.add(new HashMap<>()); } int ans = dfw(dp.size() - 1, 0, 0); if (ans > n * 2) { System.out.println(-1); } else { System.out.println(ans / 2); } } static int dfw(int idx, int m, int g) { if (idx < 0) { if (m == 0 && g == 0) { return 0; } else { return Integer.MAX_VALUE / 2; } } if (!dp.get(idx).containsKey(m)) { dp.get(idx).put(m, new HashMap<>()); } if (!dp.get(idx).get(m).containsKey(g)) { dp.get(idx).get(m).put(g, Math.min(dfw(idx - 1, m - grasses.get(idx), g + grasses.get(idx)) + grasses.get(idx), dfw(idx - 1, m + meats.get(idx), g - meats.get(idx)) + meats.get(idx))); } return dp.get(idx).get(m).get(g); } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); 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 int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }