#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, data[100001];
  cin >> n;
  int min = 0, max = 0;
  int change = 0, cp = 0;
  for (int i = 0; i < n; i++) {
    cin >> data[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (data[i] > data[i + 1]) {
      change = 1;
      cp = i + 1;
    }
    if (n > 2) {
      if (i != n - 2) {
        if (change && (data[i + 1] > data[i + 2] || data[i + 1] > data[0])) {
          cout << -1;
          return 0;
        }
      } else {
        if (change && (data[i + 1] > data[0])) {
          cout << -1;
          return 0;
        }
      }
    }
  }
  if (cp == 0) {
    cout << cp;
  } else {
    cout << (n - cp);
  }
}
