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
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
*/
import java.util.*;
import java.io.*;

   public class Ladder11
   {
      public static void main(String args[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //ez
         for(int i=0; i < N-1; i++)
         {
            if(arr[i] > arr[i+1])
            {
               //
               ArrayList<Integer> ls = new ArrayList<Integer>();
               for(int a=i+1; a < N; a++)
                  ls.add(arr[a]);
               for(int a=0; a <= i; a++)
                  ls.add(arr[a]);
               if(works(ls))
                  System.out.println(N-(i)-1);
               else
                  System.out.println(-1);
               return;
            }
         }
         System.out.println(0);
      }
      public static boolean works(ArrayList<Integer> ls)
      {
         for(int i=0; i < ls.size()-1; i++)
            if(ls.get(i) > ls.get(i+1))
               return false;
         return true;
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
   }