One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:

a1, a2, ..., an → an, a1, a2, ..., an - 1.

Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?

Input

The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.

Examples

Input

2
2 1


Output

1


Input

3
1 3 2


Output

-1


Input

2
1 2


Output

0
#include <bits/stdc++.h>
const double pi = 3.1415926535897932384626433832795;
double EPS = 10e-9;
const int INF = 2000000000;
#pragma comment(linker, "/stack:1500000000")
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
using namespace std;
void ifd() {}
void tme() {}
int n;
int a[100010], b[100010];
bool c(int t) {
  bool res = true;
  if (!t) {
    for (int i = (1); i < (n); ++i) {
      res &= a[i] >= a[i - 1];
    }
  } else {
    for (int i = (1); i < (n); ++i) {
      res &= b[i] >= b[i - 1];
    }
  }
  return res;
}
int main() {
  ifd();
  cin >> n;
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  if (c(0)) {
    cout << "0\n";
  } else {
    int pos = n - 2;
    while (a[pos] <= a[pos + 1]) pos--;
    int bpos = 0;
    for (int i = (pos + 1); i < (n); ++i) b[bpos++] = a[i];
    for (int i = (0); i < (pos + 1); ++i) b[bpos++] = a[i];
    if (c(1)) {
      cout << n - pos - 1 << "\n";
    } else {
      cout << "-1\n";
    }
  }
  tme();
  return 0;
}
