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.math.BigInteger;
import java.util.*;
import java.io.*;
 
public class solution {
    public static void merge(int arr[], int l, int m, int r) {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;
 
        /* Create temp arrays */
        int L[] = new int[n1];
        int R[] = new int[n2];
 
        /*Copy data to temp arrays*/
        for (int i = 0; i < n1; ++i)
            L[i] = arr[l + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[m + 1 + j];
 
 
        /* Merge the temp arrays */
 
        // Initial indexes of first and second subarrays
        int i = 0, j = 0;
 
        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
 
        /* Copy remaining elements of L[] if any */
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
 
        /* Copy remaining elements of R[] if any */
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
 
    // Main function that sorts arr[l..r] using
    // merge()
    public static void sort(int arr[], int l, int r) {
        if (l < r) {
            // Find the middle point
            int m = (l + r) / 2;
 
            // Sort first and second halves
            sort(arr, l, m);
            sort(arr, m + 1, r);
 
            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }
 
    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        } else return gcd(b, a % b);
    }
 
    static class pair {
        int x;
        int y;
 
        public pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
 
    public static long lcm(long a, long b) {
        long v = a * b;
        long u = gcd(a, b);
        return v / u;
    }
    public static int binomialCoeff(int n, int k)
    {
        int C[] = new int[k + 1];
 
        // nC0 is 1
        C[0] = 1;
 
        for (int i = 1; i <= n; i++)
        {
            // Compute next row of pascal
            // triangle using the previous row
            for (int j = Math.min(i, k); j > 0; j--)
                C[j] = C[j] + C[j-1];
        }
        return C[k];
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int a[] = new int[n];
        StringTokenizer st = new StringTokenizer(br.readLine());
        int min = Integer.MAX_VALUE;
        int ind = 0;
        for(int i=0;i<n;i++){
            a[i] = Integer.parseInt(st.nextToken());
        }
        for(int i=0;i<n-1;i++){
            if(a[i]>a[i+1]){
                ind = i+1;
                break;
            }
        }
        int b[] = Arrays.copyOf(a,n);
        sort(b,0,n-1);
        int j = 0;
        for(int i=ind;i<n;i++){
            if(a[i]!=b[j]){
                System.out.println("-1");
                return;
            }
            j++;
        }
        for(int i=0;i<ind;i++){
            if(a[i]!=b[j]){
                System.out.println(-1);
                return;
            }
            j++;
        }
        System.out.println((n-ind)%n);
    }
 
    static class sort implements Comparator<ArrayList<Integer>> {
 
        @Override
        public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
            int c = o2.get(1).compareTo(o1.get(1));
            if (c == 0) {
                c = o2.get(0).compareTo(o1.get(0));
            }
            return c;
        }
 
    }
}