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.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        InputStream inputStream = System.in;
        InputReader in = new InputReader(inputStream);
        Solution solver = new Solution();
        solver.solver(in);
    }

}

class Solution {

    public static boolean a(ArrayList<Integer> a, int indeks) {
        for (int i = indeks; i < a.size() - 1; i++) {
            if (a.get(i) > a.get(i + 1)) {
                return false;
            }
        }
        return true;
    }

    public static boolean small(ArrayList<Integer> a, int indeks) {
        int i = 0;
        int c = indeks - 1;
        int d = a.size() - 1;
        boolean bnr = true;
        while (true) {
            if (a.get(d) > a.get(c)) {
                return false;
            }
            if (c == 0 || d == indeks) {
                break;
            }
            i++;
            c -= i;
            d -= i;
        }

        return bnr;
    }

    public void solver(InputReader in) throws FileNotFoundException {
        ArrayList<Integer> a = new ArrayList<Integer>();
        int cases = 0;
        cases = in.nextInt();
        for (int i = 0; i < cases; i++) {
            int z = in.nextInt();
            a.add(z);
        }
        int indeks = 0;
        boolean urut = true;
        for (int i = 0; i < a.size() - 1; i++) {
            if (a.get(i) > a.get(i + 1)) {
                urut = false;
                indeks = i + 1;
                break;
            }
        }
        if (urut) {
            System.out.println("0");
        } else {
            boolean asc = a(a, indeks);
            if (asc) {
                if (a.get(a.size() - 1) > a.get(0)) {
                    System.out.println("-1");
                } else {
                    System.out.println(a.size() - indeks);
                }
            } else {
                System.out.println("-1");
            }
        }

    }
}

//prewritten input templateasdasd
class InputReader {

    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public boolean hasNext() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                String line = reader.readLine();
                if (line == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

}