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
import java.util.HashSet;
import java.util.Scanner;

public class CF454_D2_B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = nextArray(scanner, n);
        int s = minIndex(arr);
        if (s == 0) {
            int m = n - 1;
            while (m > 0 && arr[m] == arr[0]) {
                m--;
            }
            if(m <= 0){
                System.out.println(0);
                return;
            }
            s = m + 1;
        }
        if (isSorted(arr, s)) {
            System.out.println((n - s) % n);
        } else {
            System.out.println(-1);
        }
    }

    private static int minIndex(int[] arr) {
        int index = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
                index = i;
            }
        }
        return index;
    }


    private static boolean isSorted(int[] arr, int s) {
        for (int i = s; i < s + arr.length - 1; i++) {
            int index = i % arr.length;
            if (arr[index] > arr[(index + 1) % arr.length])
                return false;
        }
        return true;
    }

    public static int[] nextArray(Scanner scanner, int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        return arr;
    }
}
