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.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader scn = new InputReader(inputStream);
        PrintWriter prn = new PrintWriter(outputStream);
        II__ASC__II Mechanism = new II__ASC__II();
        Mechanism.Process(1, scn, prn);
        prn.close();
    }
}

class II__ASC__II {

    public void Process(int ASC, InputReader scn, PrintWriter prn) {
        int n = scn.nextInt(), arr[] = new int[2 * n];
        for (int i = 0; i < n; ++i) {
            arr[i] = arr[n + i] = scn.nextInt();
        }
        int cnt = 0, drop = -1;
        for (int i = 0; i < arr.length - 1; ++i) {
            if (arr[i] > arr[i + 1]) {
                ++cnt;
                if (i + 1 <= n) {
                    drop = i + 1;
                }
            }
        }
        if (cnt == 1 || drop == -1) {
            prn.println(0);
        } else if (cnt > 2) {
            prn.println(-1);
        } else {
            prn.println(n - drop);
        }
    }
}

class InputReader {

    public BufferedReader reader;
    public StringTokenizer tokenizer;

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

    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 String nextLine() throws IOException {
        return reader.readLine();
    }

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

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

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

    public Integer toInt(String s) {
        return Integer.parseInt(s.trim());
    }

    public Long toLong(String s) {
        return Long.parseLong(s.trim());
    }

    public Double toDouble(String s) {
        return Double.parseDouble(s.trim());
    }

    public int[] getIntArray(String line) {
        String s[] = line.split(" ");
        int a[] = new int[s.length];
        for (int i = 0; i < s.length; ++i) {
            a[i] = Integer.parseInt(s[i]);
        }
        return a;
    }

    public Integer[] getIntegerArray(String line) {
        String s[] = line.split(" ");
        Integer a[] = new Integer[s.length];
        for (int i = 0; i < s.length; ++i) {
            a[i] = Integer.parseInt(s[i]);
        }
        return a;
    }

    public Double[] getDoubleArray(String line) {
        String s[] = line.split(" ");
        Double a[] = new Double[s.length];
        for (int i = 0; i < s.length; ++i) {
            a[i] = Double.parseDouble(s[i]);
        }
        return a;
    }

    public Long[] getLongArray(String line) {
        String s[] = line.split(" ");
        Long a[] = new Long[s.length];
        for (int i = 0; i < s.length; ++i) {
            a[i] = Long.parseLong(s[i]);
        }
        return a;
    }

    public Long getMaxFromLongArray(Long a[]) {
        Long max = Long.MIN_VALUE;
        for (int i = 0; i < a.length; ++i) {
            if (max < a[i]) {
                max = a[i];
            }
        }
        return max;
    }

    public Long getMinFromLongArray(Long a[]) {
        Long min = Long.MAX_VALUE;
        for (int i = 0; i < a.length; ++i) {
            if (min > a[i]) {
                min = a[i];
            }
        }
        return min;
    }

    public Integer getMaxFromIntegerArray(Integer a[]) {
        Integer max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; ++i) {
            if (max < a[i]) {
                max = a[i];
            }
        }
        return max;
    }

    public Integer getMinFromIntegerArray(Integer a[]) {
        Integer min = Integer.MAX_VALUE;
        for (int i = 0; i < a.length; ++i) {
            if (min > a[i]) {
                min = a[i];
            }
        }
        return min;
    }

    long modPow(long a, long x, long p) {
        long res = 1;
        while (x > 0) {
            if (x % 2 != 0) {
                res = (res % p * a % p) % p;
            }
            a = (a % p * a % p) % p;
            x /= 2;
        }
        return res;
    }

    long modInverse(long a, long p) {
        return modPow(a, p - 2, p);
    }

    long modBinomial(long n, long k, long p) {
        long numerator = 1; // n * (n-1) * ... * (n-k+1)
        for (long i = 0; i < k; i++) {
            numerator = (numerator % p * (n - i) % p) % p;
        }
        long denominator = 1; // k!
        for (long i = 1; i <= k; i++) {
            denominator = (denominator % p * i % p) % p;
        }
        return (numerator % p * modInverse(denominator, p) % p) % p;
    }

    public long calculate(long k, long n) {
        return (modBinomial(n, k, 1000000007));
    }
}