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

public class Main {

	BufferedReader in;
	StringTokenizer str = null;
	PrintWriter out;
	
	private String next() throws Exception{
		while (str == null || !str.hasMoreElements())
			str = new StringTokenizer(in.readLine());
		return str.nextToken();
	}
	
	private int nextInt() throws Exception{
		return Integer.parseInt(next());
	}
	
	private long nextLong() throws Exception{
		return Long.parseLong(next());
	}
	
	private double nextDouble() throws Exception{
		return Double.parseDouble(next());
	}

	Random rnd;
	
	public void run() throws Exception{
		in =  new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		
		int n = nextInt();
		int []a = new int[n];
		for(int i=0;i<n;i++) a[i] = nextInt();
		int []b = new int[2*n];
		for(int i=0;i<2*n;i++){
			b[i] = a[i % n];
		}
		rnd = new Random();
		sort(0,n-1,a);

		int p[] = new int[n];
		for(int i=1;i<n;i++){
			int j = p[i-1];
			while(j > 0 && a[i] != a[j]) {
				j = p[j-1];
			}
			if (a[i] == a[j]) j++;
			p[i] = j;
		}

		int prev = 0;
		for(int i=0;i<2*n;i++){
			int j = prev;
			while(j > 0 && b[i] != a[j]) {
				j = p[j-1];
			}
			if (b[i] == a[j]) j++;
			prev = j;
			if (j == n) {
				out.println((2*n-i-1)%n);
				out.close();
				return;
			}
		}
		out.println(-1);
		out.close();
	}

	private void sort(int l, int r, int []a) {
		int m = l + rnd.nextInt(r - l + 1);
		int x = a[m];
		int i = l, j = r;
		while(i <= j) {
			while(a[i] < x) i++;
			while(a[j] > x) j--;
			if (i <= j) {
				int t = a[i];
				a[i] = a[j];
				a[j] = t;
				i++;j--;
			}
		}

		if (l < j) sort(l, j, a);
		if (i < r) sort(i, r, a);
	}
		
	public static void main(String[] args) throws Exception{
		new Main().run();
	}
}
