A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of *n* compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.

The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.

However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number.

After all the swaps each compartment should either have no student left, or have a company of three or four students.

**Input**

The first line contains integer *n* (1 ≤ *n* ≤ 10^{6}) — the number of compartments in the carriage. The second line contains *n* integers *a* _{1}, *a* _{2}, …, *a* _{n} showing how many students ride in each compartment (0 ≤ *a* _{i} ≤ 4). It is guaranteed that at least one student is riding in the train.

**Output**

If no sequence of swapping seats with other people leads to the desired result, print number “-1” (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.

**Examples **

input

5

1 2 2 4 3

output

2

input

3

4 1 1

output

2

input

4

0 3 0 4

output

0

**Solution:**

#include <iostream> #include <iomanip> #include <stdio.h> #include <set> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <memory.h> #include <string> #include <sstream> #include <cstdlib> #include <ctime> #include <cassert> using namespace std; const int inf = (int)1e9; int start[42], finish[42]; int dstart[42], dfinish[42]; int c[42], d[42]; int main() { int n; scanf("%d", &n); for (int i = 0; i <= 4; i++) c[i] = 0; int tot = 0; for (int i = 0; i < n; i++) { int foo; scanf("%d", &foo); c[foo]++; tot += foo; } int sum = 0; for (int i = 4; i >= 0; i--) { start[i] = sum + 1; finish[i] = sum + c[i]; sum += c[i]; } int ans = inf; for (int k4 = 0; 4 * k4 <= tot; k4++) { int k3 = (tot - 4 * k4); if (k3 % 3 != 0) continue; k3 /= 3; if (k4 + k3 > n) continue; for (int i = 0; i <= 4; i++) d[i] = 0; d[4] = k4; d[3] = k3; d[0] = n - k4 - k3; int sum = 0; for (int i = 4; i >= 0; i--) { dstart[i] = sum + 1; dfinish[i] = sum + d[i]; sum += d[i]; } int cur = 0; for (int i = 0; i <= 4; i++) for (int j = 0; j <= 4; j++) { int low = start[i]; if (dstart[j] > low) low = dstart[j]; int high = finish[i]; if (dfinish[j] < high) high = dfinish[j]; if (low <= high) { int diff = i - j; if (diff < 0) diff = -diff; cur += (high - low + 1) * diff; } } cur /= 2; if (cur < ans) ans = cur; } printf("%d\n", ans == inf ? -1 : ans); return 0; }