Statement
Statement language
There are children standing in a line, numbered . Initially, the children from left to right are , and every child has candies.
In one operation, choose three consecutive children. Let their numbers from left to right be . The children are very sensitive to how many candies they have, so the operation can be performed only if all three children have the same number of candies. Give one candy to each of them, then change their order from to .
Find the minimum number of operations required to sort the children into the order from left to right. If it is impossible, print .
Input
The input is given from Standard Input in the following format:
Output
Output the answer.
Constraints
- .
- ().
Subtasks
Samples
Sample 1
Input
6
4 1 3 6 2 5
Output
4
Sample 2
Input
3
2 1 3
Output
-1
Sample 3
Input
1
1
Output
0