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):
    ans = -1
    num_inv, i_inv = inversions(sequence)
    
    if num_inv == 0:
        ans = 0
    elif num_inv == 1 and sequence[-1] <= sequence[0]:
        ans = len(sequence) - i_inv - 1
    
    return ans


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