#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (auto &i : arr) cin >> i;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      int j = i + 1;
      for (; j < n; j++) {
        if (arr[j] < arr[j - 1]) {
          cout << -1 << "\n";
          return;
        }
      }
      if (arr[n - 1] <= arr[0])
        cout << n - i << "\n";
      else
        cout << -1 << "\n";
      return;
    }
  }
  cout << 0 << "\n";
}
int main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
