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.nio.Buffer;
import java.util.*;

public class codeforces
{


   // public static long fact[];
   // public static int mod=1000000007;
    public static long []st;

    public static void main(String[] args) throws IOException {

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

        int n=in.nextInt();
        int []a=new int[n];
        for (int i=0;i<n;i++)
        {
            a[i]=in.nextInt();
        }

        int c=0;
        int pos=n;
        for (int i=1;i<n;i++)
        {
            if(a[i-1]>a[i])
            {
                c++;
                pos=i;
            }

        }
        if(c==0)
            out.println("0");
        else if(c>1)
            out.println("-1");
        else 
            out.println(a[n-1]<=a[0]?n-pos:-1);




        out.close();


    }
    public static boolean check(int []a)
    {
        for (int i=1;i<a.length;i++)
        {
           if(a[i]<a[i-1])
           {
             return false;
           }
        }
        return true;

    }

    public static int nextPowerOf2(final int a) {
        int b = 1;
        while (b < a) {
            b = b << 1;
        }
        return b;
    }

    public static long []contruct(long []a)

    {
        int next2power=nextPowerOf2(a.length);
        st=new long[next2power*2-1];
        //System.out.println(size);
        // st=new long[size];
        Arrays.fill(st,Integer.MAX_VALUE);
        constructST(a,0,a.length-1,0);
        return st;



    }
    public static void constructST(long []a,int start,int end,int pos)
    {
        if(start==end)
        {
            st[pos]=a[start];
            return ;
        }
        int mid=(start+end)>>1;
        constructST(a,start,mid,pos*2+1);
        constructST(a,mid+1,end,pos*2+2);
        st[pos]=st[2*pos+1]^st[2*pos+2];


    }

       static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private InputReader.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];
            for (int i = 0; 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);
        }
    }
}