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
//package e2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class E2 {
    static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    static int readInt() throws IOException{
        int s=0;
        int sym;
        sym=in.read()-48;
        while(sym>=0&&sym<=9){
            s=s*10+sym;
            sym=in.read()-48;
        }
        return s;
    }
    static int[] a;
    static int n;
    static boolean check(int e){
        boolean c;
        int i;
        for(i=e+1; (i<n+e)&&(a[i%n]>=a[(i-1)%n]); i++);
        if(i==n+e)c=true;
        else c=false;
        return c;
    }
    public static void main(String[] args) throws IOException{
        n=readInt();
        boolean m=false;
        int i, max, min;
        in.read();
        a=new int[n];
        for(i=0; i<n; i++)
            a[i]=readInt();
        max=min=a[0];
        for(i=1; i<n; i++){
            if(a[i]>max)
                max=a[i];
            if(a[i]<min)
                min=a[i];
        }
        for(i=0; i<n; i++){
            if(a[i]==min&&a[(n+i-1)%n]==max)
                if(check(i)){
                    m=true;
                    break;
                }
        }
        if(m)System.out.println((n-i)%n);
        else System.out.println(-1);
    }
}
