#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, a[100001], pos, menor = 0x3f3f3f3f, cont = 0;
  scanf("%d", &n);
  for (int(i) = 0; (i) < (int)(n); (i)++) scanf("%d", &a[i]);
  for (int i = 0; i + 1 < n; i++)
    if (a[i] > a[i + 1]) cont++, pos = i + 1;
  if (cont == 1 and a[n - 1] > a[0]) cont = 2;
  if (cont > 1)
    puts("-1");
  else if (cont < 1)
    puts("0");
  else
    printf("%d\n", (n - pos));
  return 0;
}
