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.*;
import java.math.*;

public class Main { 
    
    public static void main(String[] args) throws java.lang.Exception { 
        Reader pm =new Reader(); 
        //Scanner pm = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n = pm.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            arr[i] = pm.nextInt();
        }
        int minH = Integer.MAX_VALUE;
        int[] mr = new int[n];
        for(int i=0;i<n;i++){
            if(arr[i] < minH){
                minH = arr[i];
                
            }
            mr[i] = minH;
        }
        /*for(int i=0;i<n;i++)
            System.out.println(mr[i]);*/
        int min = Arrays.stream(arr).min().getAsInt();
        int flag = 0;
        int no = 0;
        int steps = 0;
        int maxH = Integer.MIN_VALUE;
        for(int i=n-1;i>0;i--){
            if(arr[i-1] <= arr[i] && flag == 0){
                steps++;
                if(arr[i] > maxH)
                    maxH = arr[i];
                continue;
            }
            else if(arr[i-1] > arr[i] && flag == 0 && steps != 0){
                flag = 1;
                if(arr[n-1] > arr[i-1]){
                    no = 1;
                    break;
                }
                if(maxH > mr[i-1]){
                    //System.out.println(maxH+" "+mr[i-1]);
                    no = 1;
                    break;
                }
            }
            else if(arr[i-1] > arr[i] && flag == 0 && steps == 0){
                //System.out.println("tada");
                if(arr[i]  != min){
                    no = 1;
                    break;
                }
                //steps++;
                flag = 1;
            }
           /* else if(arr[i-1] < arr[i] && flag == 1){
                no = 1;
                break;
            }   */ 
            else if(arr[i-1] > arr[i] && flag == 1){
                no = 1;
                break;
            }
            
        }
        if(no == 1){
            System.out.println(-1);
        }
        else if(flag != 0){
            System.out.println(steps+1);
        }
        else
            System.out.println(0);
    }
    
    static class Reader { 
        final private int BUFFER_SIZE = 1 << 16; 
        private DataInputStream din; 
        private byte[] buffer; 
        private int bufferPointer, bytesRead; 
  
        public Reader() 
        { 
            din = new DataInputStream(System.in); 
            buffer = new byte[BUFFER_SIZE]; 
            bufferPointer = bytesRead = 0; 
        } 
  
        public Reader(String file_name) throws IOException 
        { 
            din = new DataInputStream(new FileInputStream(file_name)); 
            buffer = new byte[BUFFER_SIZE]; 
            bufferPointer = bytesRead = 0; 
        } 
  
        public String readLine() throws IOException 
        { 
            byte[] buf = new byte[64]; // line length 
            int cnt = 0, c; 
            while ((c = read()) != -1) 
            { 
                if (c == '\n') 
                    break; 
                buf[cnt++] = (byte) c; 
            } 
            return new String(buf, 0, cnt); 
        } 
  
        public int nextInt() throws IOException 
        { 
            int ret = 0; 
            byte c = read(); 
            while (c <= ' ') 
                c = read(); 
            boolean neg = (c == '-'); 
            if (neg) 
                c = read(); 
            do
            { 
                ret = ret * 10 + c - '0'; 
            }  while ((c = read()) >= '0' && c <= '9'); 
  
            if (neg) 
                return -ret; 
            return ret; 
        } 
  
        public long nextLong() throws IOException 
        { 
            long ret = 0; 
            byte c = read(); 
            while (c <= ' ') 
                c = read(); 
            boolean neg = (c == '-'); 
            if (neg) 
                c = read(); 
            do { 
                ret = ret * 10 + c - '0'; 
            } 
            while ((c = read()) >= '0' && c <= '9'); 
            if (neg) 
                return -ret; 
            return ret; 
        } 
  
        public double nextDouble() throws IOException 
        { 
            double ret = 0, div = 1; 
            byte c = read(); 
            while (c <= ' ') 
                c = read(); 
            boolean neg = (c == '-'); 
            if (neg) 
                c = read(); 
  
            do { 
                ret = ret * 10 + c - '0'; 
            } 
            while ((c = read()) >= '0' && c <= '9'); 
  
            if (c == '.') 
            { 
                while ((c = read()) >= '0' && c <= '9') 
                { 
                    ret += (c - '0') / (div *= 10); 
                } 
            } 
  
            if (neg) 
                return -ret; 
            return ret; 
        } 
  
        private void fillBuffer() throws IOException 
        { 
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
            if (bytesRead == -1) 
                buffer[0] = -1; 
        } 
  
        private byte read() throws IOException 
        { 
            if (bufferPointer == bytesRead) 
                fillBuffer(); 
            return buffer[bufferPointer++]; 
        } 
  
        public void close() throws IOException 
        { 
            if (din == null) 
                return; 
            din.close(); 
        } 
    }
}    