#include <bits/stdc++.h>
using namespace std;
#define ll long long

long long paletta_sort(int n, int v[]) {
    vector<int> a, b;

    for (int i = 0; i < n; i++) {
        if (i%2==0) {
            a.push_back(v[i]);
        } else {
            b.push_back(v[i]);
        }
    }

    vector<int> ca, cb;
    ca = a; cb = b;
    sort(ca.begin(), ca.end());
    sort(cb.begin(), cb.end());

    for (int i = 0; i < n/2; i++) {
        if (ca[i] > cb[i]) {
            return -1;
        }
    }

    int s = 0;
    int as = a.size();
    int bs = b.size();

    for (int i = 1; i < as; i++) {
        for (int j = 0; j < as-i; j++) {
            if (a[j+1] < a[j]) {
                int temp = a[j+1];
                a[j+1] = a[j];
                a[j] = temp;
                s++;
            }
        }
    }

    for (int i = 1; i < bs; i++) {
        for (int j = 0; j < bs-i; j++) {
            if (b[j+1] < b[j]) {
                int temp = b[j+1];
                b[j+1] = b[j];
                b[j] = temp;
                s++;
            }
        }
    }

    return s;
}
