#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> v;
  int n, num[200002], mi, loc, amt;
  mi = 99999999;
  cin >> n;
  for (int x = 0; x < n; x++) {
    cin >> num[x];
    if (num[x] < mi) {
      mi = num[x];
      loc = x;
    } else {
      if (num[x] == mi && loc == 0 && num[x - 1] > num[x]) {
        loc = x;
      }
    }
  }
  amt = n - loc;
  for (int x = loc; x < n; x++) {
    v.push_back(num[x]);
  }
  for (int x = 0; x < loc; x++) {
    v.push_back(num[x]);
  }
  sort(num, num + n);
  for (int x = 0; x < n; x++) {
    if (num[x] != v[x]) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (loc != 0) {
    cout << amt << endl;
  } else {
    cout << 0 << endl;
  }
  return 0;
}
