#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, a[250001], ind = -1, x = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      x++;
      ind = i;
    }
  }
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  } else if (x == 1) {
    if (a[n - 1] <= a[0]) {
      cout << (n - 1 - ind) << endl;
    } else {
      cout << -1 << endl;
    }
  } else {
    cout << -1 << endl;
  }
  return 0;
}
