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.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.StringTokenizer;


public class Solution{
    
    ///////////////////////////////////////////////////////////////////////////
    static class FastScanner{
        BufferedReader s;
        StringTokenizer st;
        
        public FastScanner(){
            st = new StringTokenizer("");
            s = new BufferedReader(new InputStreamReader(System.in));
        }
        
        public FastScanner(File f) throws FileNotFoundException{
            st = new StringTokenizer("");
            s = new BufferedReader (new FileReader(f));
        }
        
        public int nextInt() throws IOException{
            if(st.hasMoreTokens())
                return Integer.parseInt(st.nextToken());
            else{
                st = new StringTokenizer(s.readLine());
                return nextInt();
            }
        }
        
        public double nextDouble() throws IOException{
            if(st.hasMoreTokens())
                return Double.parseDouble(st.nextToken());
            else{
                st = new StringTokenizer(s.readLine());
                return nextDouble();
            }
        }
        
        public long nextLong() throws IOException{
            if(st.hasMoreTokens())
                return Long.parseLong(st.nextToken());
            else{
                st = new StringTokenizer(s.readLine());
                return nextLong();
            }
        }
        
        public String nextString() throws IOException{
            if(st.hasMoreTokens())
                return st.nextToken();
            else{
                st = new StringTokenizer(s.readLine());
                return nextString();
            }
            
        }
        public String readLine() throws IOException{
            return s.readLine();
        }
        
        public void close() throws IOException{
            s.close();
        }
        
    }
    
    ////////////////////////////////////////////////////////////////////
    //      Number Theory
    
    long pow(long a,long b,long mod){
        long x = 1; long y = a;
        while(b > 0){
            if(b % 2 == 1){
                x = (x*y);
                x %= mod;
            }
            y = (y*y);
            y %= mod;
            b /= 2;         
        }
        return x;
    }
    
    int divisor(long x,long[] a){
        long limit = x;
        int numberOfDivisors = 0;

        for (int i=1; i < limit; ++i) {
            if (x % i == 0) {
                limit = x / i;
                if (limit != i) {
                    numberOfDivisors++;
                }
                numberOfDivisors++;
            }
        }
        return numberOfDivisors;
    }
    
    void findSubsets(int array[]){
        
        long numOfSubsets = 1 << array.length; 
        for(int i = 0; i < numOfSubsets; i++){
                
            int pos = array.length - 1;
            int bitmask = i;

            while(bitmask > 0){     
                if((bitmask & 1) == 1)
                    ww.print(array[pos]+" ");
                bitmask >>= 1;
                pos--;
            }

            ww.println();
        }
    }
    
    
    public static int gcd(int a, int b){
        return b == 0 ? a : gcd(b,a%b);
    }
    
  
    public static int lcm(int a,int b, int c){
        return lcm(lcm(a,b),c);
    }
    
    public static int lcm(int a, int b){
        return a*b/gcd(a,b);
    }
    
    
    ////////////////////////////////////////////////////////////////////
    
    
//    private static    FastScanner s = new FastScanner(new File("input.txt"));
//    private static PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
      private static FastScanner s = new FastScanner();   
      private static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
//      private static Scanner s = new Scanner(System.in);
    @SuppressWarnings("unused")
    private static int[][] states = { {-1,0} , {1,0} , {0,-1} , {0,1} };
    
      
    //////////////////////////////////////////////////////////////////// 
      
     
    public static void main(String[] args) throws IOException{
        new Solution().solve();
    }
    
    ////////////////////////////////////////////////////////////////////
    
    
    void solve() throws IOException{
        
        int n = s.nextInt();
        int[] arr = new int[n];
        int min = Integer.MAX_VALUE;
        int index = 0;
        for(int i=0;i<n;i++){
            arr[i] = s.nextInt();
            if(arr[i] < min){
                min = arr[i];
                index = i;
            }
        }
        
        int cnt = 0;
        int ans = 0;
        int va = 0;
        int x = arr[index];
//      ww.println(index);
        if(index == 0){
            for(int i=1;i<n;i++){
                if(arr[i] >= x && ans == 0)
                    x = arr[i];
                else if(arr[i] == arr[index]){
                    ans++;
        //          ww.println(x);
                }
                else{
                    va = 1;
                    ww.println(-1);
                    break;
                }
            }
            if(va == 0 && ans == 0){
             ww.println(0);
             va = 1;
            }else if(va == 0){
                ww.println(ans);
                va = 1;
            }
        }
        else{
            ans = 1;
            while(cnt < n-1){
                cnt++;
                int in = (index+cnt)%n;
    //          ww.println(arr[in]+" "+x);
                if(arr[in] >= x && in > index){
                    ans++;
                    x = arr[in];
                }else if(arr[in] >= x)
                    x = arr[in];
                else if(arr[in] < x ){
                    ww.println(-1);
                    va = 1;
                    break;
                }
                    
            }
        }
        if(va == 0)
        ww.println(ans);
        s.close();
        ww.close(); 
    
     }
    
}