#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, max = 0, p, flag = 0, ans = 0;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (max <= arr[i] && flag == 0) {
      max = arr[i];
      p = i;
    } else
      flag = 1;
  }
  max = 0;
  for (int i = p + 1; i < n; i++) {
    if (max <= arr[i] && arr[i] <= arr[0]) {
      ans++;
      max = arr[i];
    } else {
      flag = 2;
      break;
    }
  }
  if (flag == 2)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
