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