#include <bits/stdc++.h>
using namespace std;
int a[200000];
int main() {
  int n, i, ind;
  cin >> n;
  for (i = 0; i < n; i++) cin >> a[i];
  a[-1] = -200000;
  a[n] = -200000;
  i = 0;
  while (a[i - 1] <= a[i]) i += 1;
  ind = i;
  while (a[i] <= a[i + 1]) i += 1;
  if (ind == n) {
    cout << 0;
    return 0;
  }
  if ((i < n - 1) or (a[n - 1] > a[0])) {
    cout << -1;
    return 0;
  }
  cout << n - ind;
}
