#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  scanf("%d", &n);
  int arr[n];
  for (i = 0; i < n; i++) scanf("%d", &arr[i]);
  int t = 0;
  int index = -1;
  for (i = 0; i < n; i++)
    if (arr[i] - arr[(i + 1) % n] > 0) {
      t++;
      index = i;
    }
  if (t > 1) {
    cout << "-1\n";
    return 0;
  }
  if (index == -1)
    cout << "0\n";
  else
    cout << (n - index - 1) << endl;
  return 0;
}
