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

public class CF259B {
	
	private static BufferedReader	br;
	private static StringTokenizer 	st;
	private static PrintWriter 		pw;
	// private static Timer t = new Timer();
	
	public CF259B() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(System.out);
	}
	
	String next() throws IOException {
        while (st == null || !st.hasMoreElements())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    boolean hasNext() {
        if (st != null && st.hasMoreElements())
            return true;

        try {
            while (st == null || !st.hasMoreElements())
                st = new StringTokenizer(br.readLine());
        }
        catch (Exception e) {
            return false;
        }

        return true;
    }

    String nextLine() throws IOException {
        return br.readLine();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

	void solve() throws IOException{
		
		int n = nextInt();
		int[] A = new int[n];
		for(int i = 0; i < n; i++) {
			A[i] = nextInt();
		}
		for(int i = 1; i < n; i++) {
			if(A[i-1] > A[i]) {
				for(int j = 1; j < n; j++) {
					if(A[(i+j-1) % n] > A[(i+j) % n]) {
						pw.println(-1);
						pw.flush();
						return;
					}
				}
				pw.println(n-i);
				pw.flush();
				return;
			}
		}
		pw.println(0);
		
		/*
		int n = nextInt(), min = Integer.MAX_VALUE;
		ArrayList<Integer> list = new ArrayList<>();
		
		for(int i = 0; i < n; i++) {
			int a = nextInt();
			min = Math.min(a, min);
			list.add(a);
		}
		boolean sorted = true;
		for(int i = 0; i < list.size()-1; i++) {
			if( list.get(i+1) - list.get(i) < 0) {
				sorted = false;
			}
		}
		if(sorted) {
			pw.println(0);
			pw.flush();
			return;
		}
			
		int firstindex = list.indexOf(min);
		int lastindex 	= list.lastIndexOf(min);
		int index = 0;
		if(firstindex == lastindex) {
			index = firstindex;
		}else {
			if( firstindex == 0) {
				index = lastindex;
			}else
				index = firstindex;
		}
		
		boolean right = true;
		for(int i = index; i < list.size()-1; i++ ) {
			if( (list.get(i+1) - list.get(i)) < 0 ) {
				//pw.println(right);
				right = false;
				break;
			}
		}
		//pw.println(right);
		boolean left = true;
		for(int i = index-1; i > 0; i--) {
			if( (list.get(i) - list.get(i-1)) < 0) {
				left = false;
				break;
			}
		}
		if( left && right) {
			int b = list.size() - index;
			pw.println(b);
		}else
			pw.print(-1);
		*/
		pw.flush();
	}
		
	public static void main(String[] args) throws IOException{
		 new CF259B().solve();
	 }
}