#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int j, i = 0, ind, arr[n];
  for (i = 0; i < n; i++) cin >> arr[i];
  i = 0;
  while (i < n - 1 && arr[i] <= arr[i + 1]) i++;
  i++;
  ind = i;
  if (i == n) {
    cout << "0\n";
    return 0;
  }
  while (i < n - 1 && arr[i] <= arr[i + 1]) i++;
  if (i == n - 1) {
    if (arr[n - 1] <= arr[0])
      cout << n - ind << endl;
    else
      cout << "-1\n";
  } else
    cout << "-1\n";
  return 0;
}
