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
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T>
long long powe(T base, T to) {
  long long ans = 1;
  while (to > 0) {
    if (to & 1)
      ans = (ans * base), to--;
    else
      base = (base * base), to /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
bool prime(long long n) {
  if (n == 1) return 0;
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) return 0;
  return 1;
}
template <typename T>
int binarySearch(T arr[], int start, int end, T val) {
  int mid;
  end--;
  while (start <= end) {
    mid = start + (end - start) / 2;
    if (arr[mid] == val)
      return mid;
    else if (arr[mid] < val)
      end = mid - 1;
    else
      start = mid + 1;
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tl = 1;
  while (tl--) {
    int n;
    cin >> n;
    int arr[n];
    for (auto &i : arr) cin >> i;
    ;
    int min = INT32_MAX;
    for (int i = 0; i < n; i++)
      if (arr[i] < min) min = arr[i];
    int strt = 0;
    if (arr[0] != min) {
      for (int i = 0; i < n; i++)
        if (arr[i] == min) {
          strt = i;
          break;
        }
    } else if (arr[n - 1] == min) {
      int j = n - 1;
      while (arr[j] == min) j--;
      strt = j + 1;
    }
    bool flg = 1;
    for (int i = strt + 1; i != strt; i = (i + 1) % n) {
      int j = i - 1;
      if (j == -1) j = n - 1;
      if (arr[i] < arr[j]) {
        flg = 0;
        break;
      }
    }
    cout << ((flg) ? (n - strt) % n : -1);
  }
}
