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>
using namespace std;
bool vowel(char che) {
  if (che == 'A' || che == 'E' || che == 'I' || che == 'O' || che == 'U' ||
      che == 'Y') {
    return true;
  } else {
    return false;
  }
}
const long long mod = 1e9 + 7;
long long pw(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b % 2 == 1) {
      ans *= a;
      ans %= mod;
    }
    b /= 2;
    a *= a;
    a %= mod;
  }
  return ans;
}
const int N = 2e6 + 5;
pair<long long, long long> p[N];
stack<int> sk;
string s, s1;
map<long long, long long> mp1;
char o[200][200];
bool mark[200][200], h, h1;
vector<long long> vt[N], vm, vs;
set<long long> st;
long long a[N], b[N], c[N], d[N], dp[N], dis, ps[N], tree;
void dfs(long long v, long long k) {}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t = 0, n, m, k = 0, f = 0, sum = 0, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    if (a[i] - a[i - 1] < 0) {
      vs.push_back(i);
    }
  }
  if (vs.size() == 0) {
    cout << 0 << endl;
  } else if (vs.size() == 1) {
    if (a[0] >= a[n - 1])
      cout << n - vs[0] << endl;
    else
      cout << -1 << endl;
  } else {
    cout << -1 << endl;
  }
}
