#include <bits/stdc++.h>
using namespace std;
int n;
int a[100010];
int main() {
  cin >> n;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int fir;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      if (cnt == 0) {
        cnt++;
        fir = i;
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  if (a[1] < a[n]) {
    if (cnt == 0) {
      cnt++;
      fir = 1;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  if (cnt == 0 || fir == 1) {
    cout << 0 << endl;
  } else {
    cout << n - fir + 1 << endl;
  }
  return 0;
}
