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.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class A {

	static int max = 100000;
	static boolean[] isPrime;

	// compare class //
	static class compare {
		int a;
		int b;

		public compare(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}

	// comparator class //
	static Comparator<compare> cmp = new Comparator<compare>() {
		public int compare(compare a, compare b) {
			if (a.a > b.b)
				return 1;
			else if (a.a < b.b)
				return -1;
			else
				return 0;
		}
	};

	static int getSum(long a) {

		int result = 0;
		while (a > 0) {
			result += a % 10;
			a /= 10;
		}

		return result;
	}

	static long pow(long n, int p) {
		if (p == 0)
			return 1;
		if ((p & 1) == 0)
			return pow(n * n, p >> 1);
		else
			return n * pow(n * n, p >> 1);
	}

	static int sumArray(int[] A) {
		long ans = 0;
		HashMap<Long, Integer> dp = new HashMap<Long, Integer>();

		dp.put(0L, 1);
		long curSum = 0;

		for (int i = 0; i < A.length; i++) {
			curSum += (long) A[i];
			if (dp.containsKey(curSum))
				ans += (long) dp.get(curSum);
			if (dp.containsKey(curSum))
				dp.put(curSum, dp.get(curSum) + 1);
			else
				dp.put(curSum, 1);
		}
		if (ans > 1000000000)
			ans = -1;
		return (int) ans;
	}

	static void Sieve(int N) {
		isPrime = new boolean[N + 1];
		Arrays.fill(isPrime, true);

		isPrime[0] = false;
		isPrime[1] = false;

		for (int i = 2; i * i <= N; i++) {

			if (isPrime[i]) {
				for (int j = i; i * j <= N; j++) {
					isPrime[i * j] = false;
				}
			}
		}
	}

	static char[][] arr;

	static int isValid(int i, int j) {
		if (i < arr.length && j < arr[0].length && i >= 0 && j >= 0
				&& arr[i][j] == 'o')
			return 1;
		else
			return 0;

	}

	static boolean istPalindrom(char[] word) {
		int i1 = 0;
		int i2 = word.length - 1;
		while (i2 > i1) {
			if (word[i1] != word[i2]) {
				return false;
			}
			++i1;
			--i2;
		}
		return true;
	}

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();

		int[] arr = new int[n];

		for (int i = 0; i < n; i++)
			arr[i] = s.nextInt();

		int indDec = -1;

		for (int i = 0; i < n - 1; i++) {
			if (arr[i] > arr[i + 1]) {
				indDec = i;
			}
		}

		int[] newArr = new int[n];
		int count = 0;

		for (int i = indDec + 1; i < n; i++)
			newArr[count++] = arr[i];

		for (int i = 0; i < indDec + 1; i++)
			newArr[count++] = arr[i];

		int result = -1;

		for (int i = 0; i < arr.length - 1; i++) {
			if (newArr[i] > newArr[i + 1]) {
				result = -1;
				break;
			}
			else if(indDec == -1){
				result = 0;
				break;
			}
			else {
				result = n - indDec - 1;
			}
		}
		System.out.println(result);

	}
}