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;
int main() {
  vector<int> v;
  int n, num[200002], mi, loc, amt;
  mi = 99999999;
  cin >> n;
  for (int x = 0; x < n; x++) {
    cin >> num[x];
    if (num[x] < mi) {
      mi = num[x];
      loc = x;
    } else {
      if (num[x] == mi && loc == 0 && num[x - 1] > num[x]) {
        loc = x;
      }
    }
  }
  amt = n - loc;
  for (int x = loc; x < n; x++) {
    v.push_back(num[x]);
  }
  for (int x = 0; x < loc; x++) {
    v.push_back(num[x]);
  }
  sort(num, num + n);
  for (int x = 0; x < n; x++) {
    if (num[x] != v[x]) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (loc != 0) {
    cout << amt << endl;
  } else {
    cout << 0 << endl;
  }
  return 0;
}
