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.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class CodeForces {
    static reader input = new reader();
    static PrintWriter output = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        /*
        BufferedReader bf = new BufferedReader(new FileReader("input.txt"));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
        StringTokenizer stk= new StringTokenizer(bf.readLine());;
        int n=Integer.parseInt(stk.nextToken());
        */
        B();
        output.close();
    }


    public static void A(){
    }

    public static void B(){
        int n=input.nextInt();
        int arr[]=input.nextIntArray(n);
        int min=Integer.MAX_VALUE;
        int mini=-1;
        for(int i=0;i<n;i++){
            if(arr[i]<=min){
                min=arr[i];
                mini=i;
            }
        }
        for(int i=mini;i>=1;i--){
            if(arr[i]>=arr[i-1]){
                mini--;
            }else{
                break;
            }
        }
        for(int i=mini+1;i<n;i++){
            if(arr[i]<arr[i-1]){
                output.println(-1);
                return;
            }
        }
        if(mini==0){
            output.println(0);
        }else{
            if(arr[0]<arr[n-1]){
                output.println(-1);
            }else{
                for(int i=1;i<mini;i++){
                    if(arr[i]<arr[i-1]){
                        output.println(-1);
                        return;
                    }
                }
                output.println(n-mini);
            }
        }
    }

    public static void C(){

    }

    public static void D(){

    }

    public static void E(){

    }

    public static void F(){

    }

    public static long factorial(int a){
        long ans=1;
        for(int i=1;i<=a;i++){
            ans*=i;
        }
        return ans;
    }
    public static long LCM(long a,long b){
        return (a*b)/GCD(a,b);
    }
    public static long GCD(long a,long b){
        if(a==0)
            return b;
        else if(b==0)
            return a;
        else
            return GCD(b%a,a);
    }

    public static boolean isPrime(long num){
        if(num==1)
            return false;
        else if(num==2||num==3)
            return true;
        else if(num%2==0||num%3==0)
            return false;
        else{
            for(long i=5;i*i<=num;i+=6){
                if(num%i==0||num%(i+2)==0)
                    return false;
            }
        }
        return true;
    }

    public static ArrayList<Integer> SieveofEratothenis(){
        ArrayList<Integer>ans=new ArrayList<>();
        boolean visited[]=new boolean[1000000];
        for(int i=2;i<1000000;i++){
            if(!visited[i]){
                ans.add(i);
                for(long j=(long)i*i;j<1000000;j+=i){
                    visited[(int)j]=true;
                }
            }
        }
        return ans;
    }

    public static void mergesort(long arr[],int start,int end){//start and end must be indexes
        if(start<end) {
            int mid=(start+end)/2;
            mergesort(arr,start,mid);
            mergesort(arr, mid+1, end);
            merge(arr, start,mid,end);
        }
    }
    public static void merge(long arr[],int start,int mid,int end){
        int lsize=mid-start+1,rsize=end-mid;
        long l[]=new long[lsize],r[]=new long[rsize];
        for(int i=start;i<=mid;i++){
            l[i-start]=arr[i];
        }
        for(int i=mid+1;i<=end;i++){
            r[i-mid-1]=arr[i];
        }
        int i=0,j=0,k=start;
        while(i<lsize&&j<rsize){
            if(l[i]<=r[j]){
                arr[k++]=l[i++];
            }else{
                arr[k++]=r[j++];
            }
        }
        while(i<lsize)
            arr[k++]=l[i++];
        while(j<rsize)
            arr[k++]=r[j++];
    }
}
class Trie{
    Trie a[]=new Trie[2];
}

class Value{
    int ans;
}

class Pair{
    int index,val;
    Pair(int index,int val){
        this.index=index;
        this.val=val;
    }
}

class reader {
    BufferedReader br;
    StringTokenizer st;

    public reader() {
        br = new BufferedReader(new
                InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public int[] nextIntArray(int arraySize) {
        int array[] = new int[arraySize];
        for (int i = 0; i < arraySize; i++) {
            array[i] = nextInt();
        }
        return array;
    }

    public long[] nextLongArray(int arraySize) {
        long array[] = new long[arraySize];
        for (int i = 0; i < arraySize; i++) {
            array[i] = nextLong();
        }
        return array;
    }
}
