In their free time, Ayman and William really like to play Alternato! In Alternato, there is an array with
positive integers. Ayman and William take turns, with Ayman going first. On each turn, a player can either choose any element and increment it by
, or choose any element and decrement it by
. During the process of this game, the integers may become
or negative.
As soon as the array becomes alternating (i.e. all elements are either strictly greater than their neighbors or strictly less than their neighbors), Ayman wins. However, if William can prevent that from happening within moves, he wins.
Given the starting state of the array, determine how many moves it would take for Ayman to win, if possible!
Assume both players play optimally.
Constraints
Subtask 1 [40%]
Subtask 2 [60%]
Input Specification
The first line will contain an integer .
The second line will contain space-separated integers, denoting the starting state of the array in Alternato.
Output Specification
If Ayman can win, output a single integer denoting the minimum number of moves he can win in. Otherwise, if William will win, print -1
.
Sample Input
5
2 1 3 1 1
Sample Output
1
Explanation for Sample
If Ayman selects the last element and increments it by , he wins in
move.
Comments