In their free time, Ayman and William really like to play Alternato! In Alternato, there is an array
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
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
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
Comments