B : only samples

Sample Input #1

1

0 1 1 0

Sample Output #1

-1


Sample Input #2

2

2 0 0 3

3 1 1 0

Sample Output #2

6

-1


Sample Input #3

1

7 -1 9 6

Sample Output #3

51

B1 : 200 pt.