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.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import java.io.*;
public class littlePony{
    public static void main(String[] args) {

        Scanner fi=new Scanner(System.in);
        PrintWriter pr=new PrintWriter(System.out);

        int i=0,j=0;
        int n=fi.nextInt();
        int counterInc=0;
        int counterDec=0;
        int last;
        ArrayList<Integer>al=new ArrayList<>();
        ArrayList<Integer>sortal=new ArrayList<>();
        for (i = 0; i <n ; i++) {
          int x=fi.nextInt();
          al.add(x);
          sortal.add(x);
        }
        Collections.sort(sortal);
     int flagsort=0,flag=0;


      for (i = 0; i <n ; i++) {
       if(!(sortal.get(i).equals(al.get(i))))
       {
        flagsort=-1;
        break;
       }
       else
         flagsort=1;
      }


     int flags = 0, steps = 0;

    if(flagsort==-1) {

     int id = -1;
     for (i = 0; i < n - 1; i++) {
      if (al.get(i) > al.get(i + 1)) {
       id = i + 1;
       break;
      }
     }

     ArrayList<Integer> al2 = new ArrayList<>(al.subList(id, n ));
     //pr.println(al2);
     //pr.println();

     ArrayList<Integer> listFinal = new ArrayList<>();
     listFinal.addAll(al2);
     listFinal.addAll(al);


     for (i = 0; i < n; i++) {
      if (!(listFinal.get(i).equals(sortal.get(i)))) {
         flags = -1;
         break;
      }
      else if (i == n - 1)
         flags = 1;

     }
     if(flags==1)
       steps=n-id;
    }



     if(flagsort==1)
        pr.println("0");
     else if(flags==1)
        pr.println(steps);
     else
        pr.println("-1");




        fi.close();
        pr.close();

    }

}



 class InputReader {
 private InputStream stream;
 private byte[] buf = new byte[8192];
 private int curChar, snumChars;
 private SpaceCharFilter filter;

 public InputReader(InputStream stream) {
 this.stream = stream;
 }

 public int snext() {
 if (snumChars == -1)
 throw new InputMismatchException();
 if (curChar >= snumChars) {
 curChar = 0;
 try {
 snumChars = stream.read(buf);
 } catch (IOException e) {
 throw new InputMismatchException();
 }
 if (snumChars <= 0)
 return -1;
 }
 return buf[curChar++];
 }

 public int nextInt() {
 int c = snext();
 while (isSpaceChar(c))
 c = snext();
 int sgn = 1;
 if (c == '-') {
 sgn = -1;
 c = snext();
 }
 int res = 0;
 do {
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = snext();
 } while (!isSpaceChar(c));
 return res * sgn;
 }

 public long nextLong() {
 int c = snext();
 while (isSpaceChar(c))
 c = snext();
 int sgn = 1;
 if (c == '-') {
 sgn = -1;
 c = snext();
 }
 long res = 0;
 do {
 if (c < '0' || c > '9')
 throw new InputMismatchException();
 res *= 10;
 res += c - '0';
 c = snext();
 } while (!isSpaceChar(c));
 return res * sgn;
 }

 public int[] nextIntArray(int n) {
 int a[] = new int[n+1];
 for (int i = 1; i <= n; i++)
 a[i] = nextInt();
 return a;
 }

 public String readString() {
 int c = snext();
 while (isSpaceChar(c))
 c = snext();
 StringBuilder res = new StringBuilder();
 do {
 res.appendCodePoint(c);
 c = snext();
 } while (!isSpaceChar(c));
 return res.toString();
 }

 public boolean isSpaceChar(int c) {
 if (filter != null)
 return filter.isSpaceChar(c);
 return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
 }

 public interface SpaceCharFilter {
 public boolean isSpaceChar(int ch);
 }
 }

