#include <vector>

#define ll long long

using namespace std;

struct FTree {
  vector<int> tree;
  int n;

  FTree(int n) : n(n) { tree.resize(n + 1); }

  void add(int i, int d) {
    for (i++; i <= n; i += i & -i) {
      tree[i] += d;
    }
  }

  ll query(int i) {
    ll out = 0;
    for (i++; i > 0; i -= i & -i) {
      out += tree[i];
    }
    return out;
  }
};

long long paletta_sort(int N, int V[]) {
  FTree even(N), odd(N);
  ll out = 0;
  for (int i = 0; i < N; i += 2) {
    if (V[i] % 2 == 1) {
      return -1;
    }
    out += i / 2 - even.query(V[i]);
    even.add(V[i], 1);
  }
  for (int i = 1; i < N; i += 2) {
    if (V[i] % 2 == 0) {
      return -1;
    }
    out += i / 2 - odd.query(V[i]);
    odd.add(V[i], 1);
  }
  return out;
}
