#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, first, last, val, start = 0;
  cin >> n >> last;
  first = last;
  for (int i = 1; i < n; ++i) {
    cin >> val;
    if (val < last) start = start ? -1 : n - i;
    last = val;
  }
  if (start > 0 && first < last)
    cout << -1;
  else
    cout << start;
  return 0;
}
