One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:

a1, a2, ..., an → an, a1, a2, ..., an - 1.

Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?

Input

The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.

Examples

Input

2
2 1


Output

1


Input

3
1 3 2


Output

-1


Input

2
1 2


Output

0
#
# Uian Sol Gorgonio <sol.uian@gmail.com>
# Aug 9 2014
# Little Pony and Sort by Shift
# http://codeforces.com/contest/454/problem/B
#
# Ad-hoc

def inversions(sequence):
    num_inv = 0
    i_inv = 0

    for i in xrange(len(sequence) - 1):
        if sequence[i] > sequence[i + 1]:
            num_inv += 1
            i_inv = i

    return num_inv, i_inv


def num_of_shifts(sequence):
    num_inv, i_inv = inversions(sequence)

    if num_inv == 0:
        return 0
    if num_inv > 1:
        return -1
    if num_inv == 1 and sequence[-1] <= sequence[0]:
        return len(sequence) - i_inv - 1
    
    return -1


len_seq = int(raw_input())
sequence = map(int, raw_input().split())
print num_of_shifts(sequence)

#len_seq = 2
#assert num_of_shifts([2, 1]) == 1
#len_seq = 3
#assert num_of_shifts([1, 3, 2]) == -1
#len_seq = 2
#assert num_of_shifts([1, 2]) == 0
#len_seq = 4
#assert num_of_shifts([1, 2, 3, 3]) == 0
#len_seq = 4
#assert num_of_shifts([3, 1, 1, 2]) == 3
#len_seq = 5
#assert num_of_shifts([5, 1, 2, 3, 5]) == 4
#len_seq = 3
#assert num_of_shifts([1, 2, 1]) == 1
#len_seq = 4
#assert num_of_shifts([1, 1, 1, 1]) == 0
#len_seq = 5
#assert num_of_shifts([1, 1, 2, 1, 1]) == 2
#len_seq = 5
#assert num_of_shifts([2, 2, 1, 2, 2]) == 3
#len_seq = 5
#assert num_of_shifts([4, 6, 7, 3, 5]) == -1
