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() {
  int count;
  cin >> count;
  bool isInc1 = false;
  bool isInc2 = false;
  bool isTransition = false;
  bool canBe = true;
  int transPos = -1;
  unsigned long long int min;
  unsigned long long int max;
  unsigned long long int temp;
  unsigned long long int prevNum;
  unsigned long long int a, b;
  if (count == 2) {
    cin >> a;
    cin >> b;
    if (a > b) cout << "1";
    if (a <= b) cout << "0";
    return 0;
  }
  for (int i = 0; i < count; i++) {
    cin >> temp;
    if (i == 0) {
      min = temp;
      prevNum = temp;
      continue;
    }
    if (i == 1) {
      if (prevNum > temp) {
        isTransition = true;
        isInc1 = true;
        transPos = i;
        prevNum = temp;
        continue;
      }
    }
    if (i == count - 1) {
      max = temp;
      if (!(max <= min)) {
        canBe = false;
        break;
      }
    }
    if (temp >= prevNum) {
      if (!isInc1)
        isInc1 = true;
      else if (isTransition && !isInc2)
        isInc2 = true;
    } else {
      if (isTransition) {
        canBe = false;
        break;
      } else {
        if (isInc1) {
          isTransition = true;
          transPos = i;
        } else {
          canBe = false;
          break;
        }
      }
    }
    prevNum = temp;
  }
  if (transPos == count - 1) isInc2 = true;
  if (canBe && !isTransition && !isInc2) cout << "0";
  if (!canBe) cout << "-1";
  if (canBe && isInc1 && isTransition && isInc2) cout << count - transPos;
  return 0;
}
