#include <bits/stdc++.h>
using namespace std;
bool f(int i, int j, vector<int> &a) {
  while (i + 1 <= j) {
    if (a[i] > a[i + 1]) return 0;
    i++;
  }
  return 1;
}
int main() {
  int n;
  while (cin >> n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    int p = -1;
    for (int i = 0; i + 1 < n; i++)
      if (a[i] > a[i + 1]) p = i;
    if (p == -1)
      cout << 0 << endl;
    else
      cout << (a[0] >= a[n - 1] && f(0, p, a) && f(p + 1, n - 1, a) ? n - p - 1
                                                                    : -1)
           << endl;
  }
  return 0;
}
