#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[100010], ans = 100010, id;
  bool f = false;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < ans || (f && a[i] == ans)) {
      ans = a[i];
      id = i;
      f = false;
    } else if (a[i] > ans)
      f = true;
  }
  for (int i = 1; i < n; i++) {
    if (ans > a[(i + id) % n]) {
      puts("-1");
      return 0;
    }
    ans = a[(i + id) % n];
  }
  if (id == 0) {
    puts("0");
  } else {
    cout << n - id;
  }
  return 0;
}
