#include <bits/stdc++.h>
using namespace std;
int arr[100009] = {0};
int main() {
  long long ans = 0;
  int n, cnt = 0;
  scanf("%d%d", &n, &arr[0]);
  for (int i = 1; i < n; i++) {
    scanf("%d", &arr[i]);
    if (arr[i] < arr[i - 1] && cnt != 0) {
      cout << -1 << endl;
      return 0;
    }
    if (arr[i] < arr[i - 1]) {
      cnt = i;
    }
  }
  if (cnt == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (arr[n - 1] > arr[0]) {
    cout << -1 << endl;
    return 0;
  } else
    cout << n - cnt << endl;
  return 0;
}
