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.util.*;

public class LilPony {
    
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(); //number of inputs
        int input [] = new int [n];
        int drops = 0;
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < n; i++){
            input[i] = scan.nextInt();
            min = Math.min(min, input[i]);
        }
        for(int i = 1; i < n; i++){
            int cur = input[i];
            if(cur >= input[i-1]){//greater than or equal to previous, doesn't drop
                continue;
            }
            else{
                drops++;
            }
        }
        if(drops == 0){//works without any mods
            System.out.println("0");
        }
        else if (drops == 1){//works but will need mods
            if(input[0]<input[n-1]){//aba case 
                System.out.println("-1");
            }    
            else{    
                int moves = 0;
                for(int i = n-1; i >= 0; i--){
                    moves++;
                    if(input[i]==min) {
                        int j=i-1;
                        while(input[j]==min) {
                            moves++;
                            j--;
                        }
                        break;
                    }
                }
                System.out.println(moves);
            }
        }
        else{
            System.out.println("-1");
        }
    }
}
//logic: given a string of ints, locate the smallest int in that string. Move it all the way back (keep a counter of this)
//then when it reaches max length
//push it back to position 0. compare with the original one. If the second term is different, print -1. If it works, print how many steps 
//we moved the smallest term. 

/*demonstration of logic

n = 5

input = 2 3 1 5 4 

drop = 2

1 5 4 2 3 



sorted = 1 2 3 4 5 

4 2 3 1 5 

5 4 2 3 1 

1 5 4 2 3 

compare to sorted one

second term is 5 and correct should be 2

output -1.

here's a viable case

3 4 5 1 1 

2 3 4 5 1   +1

1 2 3 4 5   +1

compare second term to correct sequence

correct.
Take in the counter (2 times)

output 4





*/