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

public class code
{
	static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            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 static void main(String args[])throws IOException
	{

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(reader.readLine());
        String[] nums = (reader.readLine()).split(" ", 0);
        int[] list = new int[n];
        for(int i = 0; i < n; i++)
        {
            list[i] = Integer.parseInt(nums[i]);
        }

        int count = 1;
        for(int i = 1; i < n; i++)
        {
            if(list[i] != list[0])
                count++;
        }

        if(count == 1)
        {
            System.out.println(0);
            return;
        }

        int min_index = 0;
        for(int i = 0; i < n; i++)
        {
            if(list[i] < list[min_index])
                min_index = i;
        }

        if(min_index==0 && list[n-1]==list[0])
        {
            min_index = n - 1;
            while(list[min_index - 1] == list[min_index])
                min_index--;
        }

        for(int i = 1; i < n; i++)
        {
            if(list[(min_index+i)%n] < list[(min_index+i-1)%n])
            {
                System.out.println(-1);
                return;
            }
        }
        System.out.println( (n - min_index)%n);
	}
}

class Pair implements Comparable<Pair>
{
	long p, h;
	public Pair(long p, long h)
	{
		this.p = p;
		this.h = h;	
	}

	public int compareTo(Pair pair)
	{
		if(this.p < pair.p)
			return -1;
		else if(this.p > pair.p)
			return 1;
		else
			return 0;
	}
}

