#include <vector>
#define ll long long
using namespace std;

ll risolvi(vector<int> &v) {
    ll output = 0;
    int L = v.size();
    // in teoria sempre O(nlogn) ma cosi è meglio
    // l'avevo fatto in un altro modo mi sa..
    vector<int> F(L+1, 0);
    for (int i = 1; i <= L; i++) F[i] = i & -i;

    vector<int> pos(L);
    for (int i = 0; i < L; i++) pos[v[i]] = i;

    for (int i = 0; i < L; i++) {
        int x = pos[i]+1;
        while (x <= L) {
            F[x]--;
            x += x & -x;
        }

        for (int j = pos[i]+1; j > 0; j -= j & -j) {
            output += F[j];
        }
    }

    return output;
}

long long paletta_sort(int N, int V[]) {
    vector<int> pari;
    vector<int> dispari;

    for (int i = 0; i < N; i++) {
        if (V[i] % 2 != i % 2) return -1;
        if (V[i] % 2 == 0) pari.push_back(V[i]/2);
        else dispari.push_back((V[i]-1)/2);
    }

    return risolvi(pari) + risolvi(dispari);
}
