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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T, typename t>
T maxm(T &x, t y) {
  if (y > x) x = y;
  return x;
}
template <typename T, typename t>
T minm(T &x, t y) {
  if (y < x) x = y;
  return x;
}
template <typename T>
long long sz(T &&x) {
  return (long long)((x).size());
}
using ll = long long;
using ld = long double;
using vi = vector<long long>;
using vb = vector<bool>;
using pii = pair<long long, long long>;
using mii = map<long long, long long>;
using vvi = vector<vector<long long>>;
using vpii = vector<pair<long long, long long>>;
using seti = set<long long>;
using setd = set<long long, greater<long long>>;
using pqmx = priority_queue<long long>;
using pqmn = priority_queue<long long, vector<long long>, greater<long long>>;
void Main() {
  long long n;
  cin >> n;
  vi a(n);
  for (long long i = 0; i < (n); ++i) cin >> a[i];
  long long ind = -1;
  for (long long i = (0); i < (n - 1); ++i) {
    if (a[i + 1] < a[i]) {
      ind = i + 1;
      break;
    }
  }
  if (ind == -1) {
    cout << 0;
    return;
  }
  for (long long i = (ind); i < (n - 1); ++i) {
    if (a[i + 1] < a[i]) {
      cout << -1;
      return;
    }
  }
  if (a[0] >= a[n - 1])
    cout << n - ind;
  else
    cout << -1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long T = 1;
  while (T--) Main();
  return 0;
}
