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.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
	private static StringTokenizer tokenizer = new StringTokenizer("");

	public static void main(String[] args) {
		try {

			int n = Integer.parseInt(stdin.readLine());
			tokenizer = new StringTokenizer(stdin.readLine());

			int numbers[] = new int[n];
			int minIndex = 0;
			int minValue = Integer.MAX_VALUE;
			int i = 0;
			while (tokenizer.hasMoreTokens()) {
				numbers[i] = Integer.parseInt(tokenizer.nextToken());
				if (numbers[i] <= minValue) {
					minValue = numbers[i];
					minIndex = i;
				}
				++i;
			}
			
			boolean allAlike = false;
			
			// if last number is of lowest value
			// ex:               1 2 1 1
			// want minIndex at:     * 
			
				
				int currIdx = minIndex;
				
				while (true) {
					if ((currIdx-1) >= 0 && numbers[currIdx-1] == numbers[currIdx]) {
						minIndex = currIdx-1;
						currIdx = currIdx-1;
						
						if (currIdx == 0) {
							allAlike = true;
						}
					} else {
						break;
					}
				}
			
			// ex:                1 1 1 2 2
			// want minIndex at:  *
			if (numbers[0] == minValue && numbers[n-1] != minValue) {
				minIndex = 0;
			}
			
			//System.out.println("minIndex = " + minIndex);
			
			boolean OK = true;

			int currIndex = minIndex;

			for (int j = 0; j < n-1; ++j) {
				//System.out.println(numbers[currIndex] + " vs " + numbers[(currIndex + 1) % n]);
				if (numbers[currIndex] <= numbers[(currIndex + 1) % n]) { // OK

					currIndex = (currIndex + 1) % n;
				} else { // not satisfying circular ascending array property
					OK = false;
					break;
				}
			}

			if (OK) { // has circular ascending array property
				if (minIndex == 0)
					System.out.println("0");
				else if (allAlike)
					System.out.println("0");
				else
					System.out.println(n-minIndex);
			} else {
				System.out.println("-1");
			}

		} catch (Exception e) {

		}
	}
}