#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  deque<int> a;
  int count = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    a.push_back(temp);
  }
  bool flag = true;
  for (int i = 1; i < n; i++) {
    if (a[i] == a[i - 1]) {
      continue;
    } else {
      flag = false;
      break;
    }
  }
  if (flag == true) {
    cout << "0";
  } else {
    flag = true;
    int c = n - 1;
    while (c--) {
      if (a[n - 1] <= a[0]) {
        int temp = a[n - 1];
        a.pop_back();
        a.push_front(temp);
        count++;
      } else {
        break;
      }
    }
    for (int i = 1; i < n; i++) {
      if (a[i] >= a[i - 1]) {
        continue;
      } else {
        flag = false;
        break;
      }
    }
    if (flag == true) {
      cout << count;
    } else {
      cout << "-1";
    }
  }
}
