#include <bits/stdc++.h>
using namespace std;
int n;
int a[100001];
int aa, bb, ai, bi, flag;
int main() {
  cin >> n;
  int cnt = -1;
  int min = INT_MAX;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] < min) {
      min = a[i];
      ai = i;
    }
    if (i > 0 && a[i - 1] > min && a[i] <= min) {
      min = a[i];
      ai = i;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[(i + ai) % n] > a[(i + ai + 1) % n]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << (n - ai) % n << endl;
  return 0;
}
