#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n;
  cin >> n;
  long long arr[n];
  for (auto &i : arr) cin >> i;
  long long dis = 0;
  long long c = 0;
  for (long long i = 0; i < n; i++) {
    if (arr[i] > arr[(i + 1) % n]) {
      dis++;
      if (dis > 1) {
        cout << -1 << endl;
        return 0;
      }
      c = i + 1;
    }
  }
  cout << (n - c) % n << endl;
  return 0;
}
