#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int x;
  cin >> x;
  int temp = x;
  int start = x;
  int res = 0;
  int index = -1;
  for (int i = 1; i < n; i++) {
    cin >> x;
    if (index >= 0) {
      if (x < temp) {
        res = -1;
      } else {
        if (x > start) {
          res = -1;
        }
      }
    } else {
      if (x < temp) {
        if (x > start) {
          res = -1;
        } else {
          index = i;
        }
      }
    }
    temp = x;
  }
  if (res != -1) {
    if (index != -1)
      cout << n - index << endl;
    else
      cout << 0 << endl;
  } else {
    cout << res << endl;
  }
}
