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
#### Answer to <https://codeforces.com/problemset/problem/251/A>
# import math

# count, bounds = [int(x) for x in input().split(" ")]
# total = 0

# left_index = 0
# right_index = 2
# current = 0

# lst = [int(x) for x in input().split(" ")]
# if lst[0] < 0:
#     delta = 1 - lst[0]
#     lst = [x + delta for x in lst]

# while right_index < len(lst):
#     if lst[right_index] - lst[left_index] <= bounds:
#         right_index += 1
#         continue
#     elements = right_index - left_index - 1
#     total += elements * (elements - 1) / 2
#     left_index += 1

# while left_index < len(lst) - 2:
#     if lst[count-1] - lst[left_index] <= bounds:
#         elements = count - left_index - 2
#         total += elements * (elements + 1) / 2
#         left_index += 1
# print(int(total))

# ### Answer to <https://codeforces.com/contest/38/problem/C>
# count, bounds = [int(x) for x in input().split(" ")]
# lst = [int(x) for x in input().split(" ")]
# lst.sort()


# right_index = count - 1
# left_index = 0
# highest = 0
# current_num = -1

# # Find left most index where number is greater than or equal to bounds
# for index, i in enumerate(lst):
#     if i < bounds:
#         left_index += 1
#         continue
#     break

# while right_index >= left_index:
#     # Same number as before, no need to check
#     if lst[left_index] == current_num:
#         left_index += 1
#         continue

#     # Different Number
#     current_num = lst[left_index]

#     for j in range(current_num, bounds-1, -1):
#         current = 0
#         for i in range(left_index, right_index + 1):
#             # Add maximum possible
#             current += (lst[i] // j) * j
#         # print("L:", left_index, "| R:", right_index, "current:", current)
#         if current > highest:
#             highest = current
#     left_index += 1

# print(highest)

#### <https://codeforces.com/problemset/problem/617/B>
# def solve():
#     numbers = int(input())
#     lst = [int(x) for x in input().split(" ")]
#     add = sum
#     if add(lst) <= 1:
#         print(add(lst))
#         return
#     elif add(lst) == len(lst):
#         print(1)
#         return

#     left_index = lst.index(1) # First occurence of a nut
#     right_index = left_index

#     for i in range(left_index, len(lst)):
#         if lst[i] == 1:
#             right_index = i
#             break
    
#     total = 1
#     while right_index < len(lst):
#         if lst[right_index] == 1:
#             delta = right_index - left_index
#             total *= max(1, delta)
#             left_index = right_index
#         right_index += 1
#     print(total)
# solve()

#### <https://codeforces.com/contest/617/problem/C> UNSOLVED
# def solve():
#     total = 0
#     n, r_x, r_y, s_x, s_y = list(map(int, input().split(" ")))
#     flowers = []
#     for i in range(n):
#         flowers.append(list(map(int, input().split(" "))))

#     r_list = []
#     s_list = []
#     flowers_list = []
#     for index, i in enumerate(flowers):
#         distance_r = (r_x - i[0])**2 + (r_y - i[1])**2
#         distance_s = (s_x - i[0])**2 + (s_y - i[1])**2
#         # r_list.append((index, distance_r))
#         # s_list.append((index, distance_s))

#         flowers_list.append((index, distance_r, "R"))
#         flowers_list.append((index, distance_s, "S"))

#     flowers_list.sort(key=lambda x:x[1])


#     # r_list.sort(key=lambda x:x[1])
#     # s_list.sort(key=lambda x:x[1])

#     # lowest = int(s_list[-1][1]) + 1
#     # for index, j in enumerate(s_list):
#     #     current = 0

#     #     # Show all the flowers that are being given to r
#     #     r_tmp = []
#     #     for i in range(index, len(s_list)):
#     #         r_tmp.append(s_list[i][0])


#     # print(r_list)
#     # print(s_list)
#     print(flowers_list)
#     flower_freq = {}
#     for i in range(n):
#         flower_freq[i] = 0


#     val1 = flowers_list[-1][0]
#     val2 = 0
#     if flowers_list[-1][-1] == 'S':
#         highest = 'R'
#     else:
#         highest = 'S'


#     for i in range(n):
#         flowers_freq[flowers_list[i][0]] += 1
#         if flowers_list[i][2] != highest:
#             val2 = flowers_list[i][0]
#     # total = r1 + r2
#     # print(total)
# solve()

#### Solution <https://codeforces.com/problemset/problem/615/A>
# def solve():
#     groups, lights = list(map(int, input().split(" ")))
#     lst = []

#     for i in range(groups):
#         group = list(map(int, input().split(" ")))
#         num = group.pop(0)
#         lst = lst + group
#     lst = list(set(lst))
#     if len(lst) == lights:
#         print("YES")
#     else:
#         print("NO")
 

# solve()

#### Solution <https://codeforces.com/problemset/problem/614/A>
# def solve():
#     l, r, k = list(map(int, input().split(" ")))
#     lst = []
#     count = 0
#     while k ** count <= r:
#         if k ** count >= l:
#             lst.append(k ** count)
#         count += 1
#     if len(lst) == 0:
#         print(-1)
#     else:
#         print(" ".join([str(x) for x in lst]))

# solve()

# def solve():
#     n = int(input())
#     words = {}
#     for i in range(n):
#       word = input()
#       while "u" in word:
#           word = word.replace("u", "oo")
#       while "kkh" in word:
#           word = word.replace("kkh", "kh")
#       if "kh" in word:
#           word = word.replace("kh", "h")
#       if word not in words:
#           words[word] = 1
#       else:
#           words[word] += 1
#     print(len(words))
# solve()


#### Pheonix and Balance
# def solve():
#     t = int(input())
#     add = len
#     for i in range(t):
#         n = int(input())
#         if n <= 2:
#             print(n)
#             continue

#         lst = [int(2**i) for i in range(1, n+1)]
#         pile1 = []
#         pile2 = []
#         pile1.append(lst.pop(len(lst) - 1))
#         count1 = pile1[0]
#         count2 = 0


#         while add(lst) > 0:
#             while add(lst) > 0 and count2 < count1 and add(pile2) < (n//2):
#                 pile2.append(lst.pop(add(lst) - 1))
#                 count2 += pile2[-1]
                
#             if add(pile2) == n//2:
#                 while add(lst) > 0:
#                     pile1.append(lst.pop(add(lst) - 1))
#                     count1 += pile1[-1]
#             else:
#                 pile1.append(lst.pop(add(lst) - 1))
#                 count1 += pile1[-1]
#         print(abs(count2 - count1))

# solve()

#### Pheonix and Distribution
# def solve():
#     t = int(input())
#     for i in range(t):
#         n, k = list(map(int, input().split(" ")))
#         word = sorted(input())
#         add = len

#         # Case k = 1, i.e. take the whole string
#         if k == 1:
#             print("".join(word))
#             continue

#         lst = []
#         # Put a list for each substring
#         for j in range(k):
#             lst.append([])

#         # Check if all the first k letters are the same
#         if add(set(word[:k])) != 1:
#             print(word[k-1])
#             continue

#         # Put the first k letters into the lists
#         for m in range(k):
#             lst[m].append(word.pop(0))
        
#         # Check for 2    or more distinct letters
#         if add(set(word)) != 1:
#             print("".join(lst[0] + word))
#             continue

#         # Less than or k remaining letters
#         elif add(word) <= k:
#             lst[0].append(word[0])
#             print("".join(lst[0]))
#             continue

#         while add(word) > k:
#             for m in range(k):
#                 lst[m].append(word.pop(0))

#         lst[0].append(word[0])
#         print("".join(lst[0]))

# solve()


# def solve():
#     t = int(input())
#     for i in range(t):
#         n = int(input())
#         count = 0
#         while n > 2**count:
#             count += 1
#         print(count)
#         if n == 2:
#             print(0)

# solve()


# Codejam 1C Q1
# def main():
#     t = int(input())
#     for i in range(1, t+1):
#         solve(i)

# def solve(i):
#     nextline = input().split(" ")
#     x, y = [int(z) for z in nextline[:2]]
#     coordinates = list(nextline[-1])

#     for index, pos in enumerate(coordinates):
#         # Move Peppurr
#         if pos == 'S':
#             y -= 1
#         elif pos == 'N':
#             y += 1
#         elif pos == 'W':
#             x -= 1
#         else:
#             x += 1

#         # See if possible to get that location
#         moves = index + 1
#         if moves >= abs(x) + abs(y):
#             print("Case #{}: {}".format(i, moves))
#             return


#     print("Case #{}: IMPOSSIBLE".format(i))

# main()

# import math

# def main():
#     t = int(input())
#     for i in range(1, t+1):
#         solve(i)

# def solve(case):
#     slices, diners = list(map(int, input().split(" ")))
#     parts = list(map(int, input().split(" ")))
#     parts.sort()

#     if sum(parts) < diners:
#         parts = [m*diners for m in parts]

#     max_angle = math.ceil(sum(parts) / diners)
#     # print(max_angle)

#     slice_freq = {}
#     for i in parts:
#         if i not in slice_freq:
#             slice_freq[i] = 1
#         else:
#             slice_freq[i] += 1

#     angle_freq = sorted(slice_freq.items(), key=lambda x:x[1])

#     unique_parts = list(set(parts))
#     lowest_cuts = diners
#     for index, m in enumerate(angle_freq[::-1]):
#         highest_freq = m[1]
#         slice_val = m[0]

#         # More than enough equal slices for diners
#         if highest_freq >= diners:
#             print("Case #{}: {}".format(case, 0))
#             return

#         # Calculate how many slices needed
#         needed = diners - highest_freq
#         cuts = 0

#         # First check for multiples of our slice
#         tmp = slice_val
#         while tmp <= unique_parts[-1]:
#             if tmp + slice_val in unique_parts:
#                 if needed - (tmp + slice_val) / slice_val >= 0:
#                     needed -= (tmp + slice_val) / slice_val
#                     cuts += (tmp + slice_val) / slice_val - 1
#                 else:
#                     cuts += needed
#                     needed = 0
#                     break

#             tmp += slice_val    


#         # Find all the slices larger than our own
#         slice_index = unique_parts.index(slice_val) + 1
#         while needed > 0 and slice_index < len(unique_parts):
#             if unique_parts[slice_index] % slice_val == 0:
#                 slice_index += 1
#                 continue

#             amount = unique_parts[slice_index] // slice_val
#             if amount >= needed:
#                 cuts += needed
#                 needed = 0
#                 break

#             cuts += amount
#             needed -= amount
#             slice_index += 1

#         if needed == 0 and cuts < lowest_cuts:
#             lowest_cuts = int(cuts)


#     small_cut = diners
#     for j in range(1, max_angle + 1):
#         if j in unique_parts:
#             continue

#         current_val = j

#         # Calculate how many slices needed
#         needed = diners
#         cuts = 0

#         # First check for multiples of our slice
#         tmp = current_val
#         while tmp <= unique_parts[-1]:
#             if tmp + current_val in unique_parts:
#                 if needed - (tmp + current_val) / current_val >= 0:
#                     needed -= (tmp + current_val) / current_val
#                     cuts += (tmp + current_val) / current_val - 1
#                 else:
#                     cuts += needed
#                     needed = 0
#                     break

#             tmp += current_val

#         # Find all the slices larger than our own
#         slice_index = 0
#         while needed > 0 and slice_index < len(unique_parts):
#             if unique_parts[slice_index] % slice_val == 0 or unique_parts[slice_index] < current_val:
#                 slice_index += 1
#                 continue

#             amount = unique_parts[slice_index] // slice_val
#             if amount >= needed:
#                 cuts += needed
#                 needed = 0
#                 break

#             cuts += amount
#             needed -= amount
#             slice_index += 1

#         if needed == 0 and cuts < small_cut:
#             small_cut = int(cuts)

        
#     print("Case #{}: {}".format(case, min(lowest_cuts, small_cut)))

# main()

# import math

# def main():
#     t = int(input())
#     for i in range(1, t+1):
#         solve(i)

# def solve(case):
#     slices, diners = list(map(int, input().split(" ")))
#     parts = list(map(int, input().split(" ")))
#     parts.sort()

#     if sum(parts) < diners:
#         parts = [m*diners for m in parts]

#     max_angle = math.ceil(sum(parts) / diners)
#     # print(max_angle)

#     lowest_cuts = diners
#     for j in range(1, max_angle + 1):
#         slice_val = j
#         # 1) Find out if it's possible to get enough parts for diners

#         # Too high slices
#         if slice_val > parts[-1]:
#             break

#         count = 0
#         for k in parts:
#             count += k // slice_val

#         if count < diners:
#             continue

#         # 2) Find out minimum amount of cuts needed
#         parts_dup = parts[:]
#         multiples = [k for k in parts_dup if k % slice_val == 0 and k > slice_val].sort()


#         # Calculate how many slices needed
#         needed = diners
#         cuts = 0

#         if multiples != None:
#             for m in multiples:
#                 if needed - m / slice_val > 0:
#                     needed -= m / slice_val
#                     cuts += m / slice_val - 1
#                 elif needed - m / slice_val == 0:
#                     needed -= m / slice_val
#                     cuts += m / slice_val - 1
#                     break
#                 else:
#                     cuts += needed
#                     needed = 0
#                     break

#         # All diners satisfied
#         if needed == 0:
#             if cuts < lowest_cuts:
#                 lowest_cuts = cuts
#             break

#         for n in parts_dup:
#             if n % slice_val == 0:
#                 continue

#             amount = n // slice_val
#             if amount > needed:
#                 cuts += amount
#                 needed -= amount
#             elif amount == needed:
#                 cuts += amount
#                 needed = 0
#                 break
#             else:
#                 cuts += needed
#                 needed = 0
#                 break

#         if needed == 0 and cuts < lowest_cuts:
#             lowest_cuts = cuts

        
#     print("Case #{}: {}".format(case, lowest_cuts))

# main()


def solve():
    nums = int(input())
    numbers = list(map(int, input().split(" ")))


    # Case 1)
    # Sorted = print 0
    if numbers == sorted(numbers):
        print(0)
        return

    # Case 2) Sorted from highest to lowest
    highest_index = numbers.index(max(numbers))
    while highest_index < len(numbers) - 1:
        if numbers[highest_index] == numbers[highest_index + 1]:
            highest_index += 1
        else:
            break

    lst1 = numbers[:highest_index+1]
    lst2 = numbers[highest_index+1:]

    if lst1 == sorted(lst1) and lst2 == sorted(lst2) and lst2[-1] <= lst1[0]:
        print(len(lst2))
    else:
        print(-1)


solve()
