Maratona de Programação da SBC Brazil
The Conductor of the choir is planning to take part in the famous Brazilian Choir Week, and therefore she wants the choir to rehearse a new song, described as follows:
• each member of the choir starts singing one note, and only changes the note when determined by the Conductor;
• at the end of each bar measure, the Conductor determines that exactly two singers change the note they are singing: one singer starts to sing the note immediately above the note she sang, and another singer starts to sing the note immediately below the note she sang;
• the song finishes at the end of the first bar measure in which all singers are singing the same note.
The Conductor already has several ideas of how to distribute the notes among choir members at the beginning of the song, in order to create the desired effect. However, she is worried about whether, given a note distribution for the singers, it is possible to reach the end of the song in the way she wants (all singing the same note). And, if that is possible, she wants to know the minimum number of bar measures the song can have. Can you help her?
The input contains several test cases. The first line of a test case contains an integer N (2 ≤ N ≤ 104) indicating the number of members of the choir. Notes are indicated by integers. The second line contains N integers, indicating the note (−105 ≤ notai ≤105) where 0 ≤ i ≤ N−1, that each singer must sing at the beginning of the song. Notes are given in non-decreasing order (notai ≤notai+1).
For each test case, print a line containing a single integer indicating the minimum number of bar measures the song can have. If it is not possible to have all members singing the same note, then print the value−1.
|Sample Input||Sample Output|