#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  int i, k = 0, n, a, b, c;
  bool p = 1, q = 1;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    if (i == 0) c = a;
    if (a < b && i > 0 && p) {
      p = 0;
      k = i;
      if (a > c) q = 0;
    } else if (a > b && p == 0 && i > 0 && q == 1) {
      if (a > c) q = 0;
    } else if (a < b && p == 0 && i > 0 && q == 1) {
      q = 0;
    }
    b = a;
  }
  if (p)
    cout << k << endl;
  else if (q == 0)
    cout << "-1" << endl;
  else
    cout << n - k << endl;
  return 0;
}
