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
/* Harish Anumula  */
import java.util.*;
import java.io.*;
//import java.math.*;

public class MAXEINSTLE
{
    public static void main (String[] args) throws java.lang.Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        FastScanner scan = new FastScanner(br);
        Task solver = new Task();
        solver.solve(scan,pw);
        pw.close();
    }
}



class Task{
    public void solve(FastScanner scan , PrintWriter pw) throws java.lang.Exception{
        int n = scan.nextInt();
        int arr[] = new int[n];
        int first = n;
        for(int i = 0;i<n;i++){
            arr[i] = scan.nextInt();
            if(i>0 && arr[i]<arr[i-1]){
                first = i;
            }
        }
        int[] temp = new int[n];
        for(int i = first;i<n;i++){
            temp[i-first] = arr[i];
        }
        for(int i = 0;i<first;i++){
            temp[n-first+i] = arr[i];
        }
        Arrays.sort(arr);
        if(Arrays.equals(arr, temp) || first == n){
            pw.println(n-first);
        }
        else{
            pw.println(-1);
        }
    }
}



class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner(BufferedReader br) {
        this.br = br;
    }

    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
    public String nextString() throws Exception {
        return br.readLine();
    }
}