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.*;
public class Template {
	//static int cnt=0;
static Scanner sc=new Scanner(System.in);
	static ArrayList<Integer> v=new ArrayList();;
	static int max=0;
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
	
	public static void main (String[] args) throws java.lang.Exception{
int size=sc.nextInt();
boolean sorted=true;
ArrayList<Integer> arr=new ArrayList();
int max=Integer.MIN_VALUE;
arr.add(sc.nextInt());
max=arr.get(0);
int indfst=0;
int indlst=0;
for(int i=1;i<size;i++) {
	int n=sc.nextInt();
	arr.add(n);
	if(n>max) {
		max=n;
		indfst=i;
	}
	if(n>=max){
	    max=n;
	    indlst=i;
	}
	if(sorted) {
	if(arr.get(i-1)>n) {
		sorted=false;
	}
	}
	
}

if(sorted)
	System.out.println("0");
else if(arr.get(size-1)>arr.get(0))
	System.out.println("-1");

else {
    boolean np=false;
if(indfst!=0 || indfst==indlst){
    for(int i=indlst+1;i<size-1;i++){
        if(arr.get(i+1)<arr.get(i)){
            np=true;
        }
    }
    for(int i=0;i<indlst;i++){
        if(arr.get(i+1)<arr.get(i)){
            np=true;
        }
    }
    if(np)
    System.out.println("-1");
    else
    System.out.println(size-indlst-1);
}

else{
    boolean npp=false;
    int cind=0;
    for(int i=0;i<size-1;i++){
        if(arr.get(i+1)!=arr.get(i)){
          cind=i;
          break;
        }
    }
    
    for(int j=cind+1;j<size-1;j++){
        if(arr.get(j+1)<arr.get(j)){
            npp=true;
            break;
        }
    }
    if(npp)
    System.out.println("-1");
    else
    System.out.println(size-cind-1);
}
    
}
	


		
	
	}
	
	



	
//}
	
	
	
	
	
		
	
	
	

		
	
	




	
	

		
		
	
		
	

	
	
	

		
	
	
	
	
static long gcd(long a,long b) {
		
	
		if(b==0) {
		return a;
		}
			
		
	return gcd(b,a%b);
	
	}
	
	
	
	
	
	public static int s2i(String s) {
		return Integer.parseInt(s);
	}
	

	
	
	public static int LeastIntFn(int a,int b) {
		if((b%a)==0) {
			return (int)b/a;
		}
		return (int)(b/a)+1;
	}
	
	
	
	public static ArrayList<Long> arrInpLong(int n){
		ArrayList<Long> arr=new ArrayList();
		for(int i=0;i<n;i++) {
		arr.add(sc.nextLong());	
		}
		return arr;
	}
	public static ArrayList<String> arrInpStr(int n){
		ArrayList<String> arr=new ArrayList();
		for(int i=0;i<n;i++) {
					arr.add(sc.nextLine());	
		}
		return arr;
	}
	
	public static ArrayList<Integer> arrInpInt(int n){
		ArrayList<Integer> arr=new ArrayList();
		for(int i=0;i<n;i++) {
			int k=sc.nextInt();
			if(k>max)
				max=k;
		arr.add(k);	
		}
		return arr;
	}
	
	
	public static void printArrInt(ArrayList<Integer> arr) {
		for(int i=0;i<arr.size();i++) {
			System.out.print(arr.get(i)+" ");
	}	
		}
	
	
	public static int min(int a,int b,int c) {
		int min1=Math.min(a, b);
		return Math.min(min1,c);
	}
}