#include <bits/stdc++.h>
using namespace std;
int main() {
  int num[100005];
  int n;
  bool flag2 = false;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  int temp = num[0];
  int t1 = 1, t2 = 1;
  if (num[1] >= num[0]) {
    t1 = 2;
  } else {
    flag2 = true;
    t2 = 2;
  }
  for (int i = 1; i <= n - 2; i++) {
    if (!flag2) {
      if (num[i] <= num[i + 1]) {
        t1++;
      } else {
        if (temp >= num[i + 1]) {
          flag2 = true;
          t2 = 2;
        } else
          break;
      }
    } else {
      if (num[i] <= num[i + 1] && temp >= num[i + 1])
        t2++;
      else
        break;
    }
  }
  if (t1 + t2 - 1 == n)
    cout << t2 - 1 << endl;
  else
    cout << "-1" << endl;
  return 0;
}
