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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;


public class Main {
static class FastScanner {

BufferedReader reader;
StringTokenizer tokenizer;

public FastScanner(InputStream stream) {
this.reader = new BufferedReader(new InputStreamReader(stream));
}

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

public int[] nextIntegerArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextInt();
}
return a;
}

public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextLong();
}
return a;
}

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

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

public long nextLong(int radix) throws IOException {
return Long.parseLong(next(), radix);
}

public double nextDouble() throws IOException {
return Double.parseDouble(next());
}

public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}

public BigInteger nextBigInteger(int radix) throws IOException {
return new BigInteger(next(), radix);
}

public String next() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
return this.next();
}
return tokenizer.nextToken();
}

public void close() throws IOException {
this.reader.close();
}
}

                   //SOLUTION //


	FastScanner in = new FastScanner(System.in);
	PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) throws IOException{
    Main a=new Main();
    a.solve();
    }//void main
	
	void solve() throws IOException{
	     int n=in.nextInt();
	     int[] first=new int[n];
	     
	     for(int i=0; i<n; i++)
	        first[i]=in.nextInt();
	     
	     int count=0,ref=-9,temp=0;
	     for(int i=0; i<n-1;i++){
	    	 if(ref==-9 && first[i]>first[i+1])
	    		 ref=i;
	     }
	     if(ref==-9){
	    	 out.println(0);
	    	 out.close();
	    	 return;
	     }int i;
	     temp=first[0];
	     for(i=n-1;i>ref;i--){
	    	 if(temp>=first[i]){
	    		 count++;
	    		temp=first[i];
	    	 }
	    	 else {out.println(-1);out.close();return;}
	     }
	     out.println(count);
	     in.close();
	     out.flush();
		 out.close();
	}
}
