#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s, f = 0;
  cin >> n;
  vector<int> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  for (long long i = 0; i < n - 1; i++) {
    if (v[i] > v[i + 1]) s = i, f++;
  }
  if (v[n - 1] > v[0]) s = n - 1, f++;
  if (f > 1)
    cout << -1;
  else if (f == 0)
    cout << 0;
  else
    cout << n - s - 1;
}
