Language: C++

#agma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
 
#include <bits/stdc++.h>
 
#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
 
using namespace std;
using ll = int64_t;
 
struct Card {
  int A, B;
};
 
struct Range {
  int left, right;
};
 
int N;
vector<Card> cards;
int check_answer = numeric_limits<int>::max();
map<pair<int, vector<int>>, int> min_steps;
 
bool sorted(int mask, vector<int> order) {
  vector<int> numbers(N);
  for (int i = 0; i < N; i++) {
    int j = order[i];
    if ((mask >> j) & 1) {
      numbers[i] = cards[j].B;
    } else {
      numbers[i] = cards[j].A;
    }
  }
  return is_sorted(all(numbers));
}
 
void rec(int mask, vector<int> order, int steps) {
  if (steps >= check_answer) {
    return;
  }
  pair<int, vector<int>> key = {mask, order};
  auto it = min_steps.find(key);
  if (it != min_steps.end() && steps >= it->second) {
    return;
  }
  min_steps[key] = steps;
  if (sorted(mask, order)) {
    check_answer = steps;
    return;
  }
  for (int i = 0; i < N - 1; i++) {
    int lhs = order[i], rhs = order[i + 1];
    int new_mask = mask;
    new_mask ^= (1 << lhs);
    new_mask ^= (1 << rhs);
    auto new_order = order;
    swap(new_order[i], new_order[i + 1]);
    rec(new_mask, new_order, steps + 2);
  }
}
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
 
  cin >> N;
  cards.resize(N);
  for (auto& card : cards) {
    cin >> card.A;
  }
  for (auto& card : cards) {
    cin >> card.B;
  }
  vector<int> order(N);
  iota(all(order), 0);
  // rec(0, order, 0);
  vector<Range> ranges(N);
  vector<int> used(N);
  int min_operations = numeric_limits<int>::max();
  // int result_mask = -1;
  for (int mask = 0; mask < (1 << N); mask++) {
    if (__builtin_popcount(mask) % 2 != 0) {
      continue;
    }
    for (int i = 0; i < N; i++) {
      if ((mask >> i) & 1) {
        order[i] = cards[i].B;
      } else {
        order[i] = cards[i].A;
      }
    }
    sort(all(order));
    // cerr << "mask: " << mask << endl;
    for (int i = 0; i < N; i++) {
      int number;
      if ((mask >> i) & 1) {
        number = cards[i].B;
      } else {
        number = cards[i].A;
      }
      int left = N, right = -1;
      for (int j = 0; j < N; j++) {
        if (order[j] == number) {
          left = min(left, j);
          right = max(right, j);
        }
      }
      // cerr << "i: " << i << " " << number << " " << left << " " << right << endl;
      assert(left <= right);
      ranges[i] = Range{left, right};
    }
    int operations = 0;
    bool fail = false;
    fill(all(used), -1);
    for (int i = 0; i < N && !fail; i++) {
      int odd = (mask >> i) & 1;
      int closest = -1;
      // cerr << "w: " << i << " " << odd << endl;
      for (int j = ranges[i].left; j <= ranges[i].right; j++) {
        if (used[j] == -1 && abs(j - i) % 2 == odd) {
          // cerr << "t: " << j << endl;
          if (closest == -1) {
            closest = j;
          }
        }
      }
      // cerr << "c: " << i << " " << closest << endl;
      if (closest == -1) {
        fail = true;
      } else {
        used[closest] = i;
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (used[i] > used[j]) {
          operations += 2;
        }
      }
    }
    // cerr << "ans: " << fail << " " << operations << endl;
    if (!fail && operations < min_operations) {
      min_operations = operations;
      // result_mask = mask;
    }
  }
  // cerr << min_operations << " " << check_answer << endl;
  if (min_operations == numeric_limits<int>::max()) {
    cout << -1 << endl;
    return 0;
  }
  // assert(result_mask != -1);
  assert(min_operations % 2 == 0);
  cout << min_operations / 2 << endl;
 
 
 
  return 0;
}

Compiler output:
a.cc:1:2: error: invalid preprocessing directive #agma; did you mean #pragma?
    1 | #agma GCC optimize("O3")
      |  ^~~~
      |  pragma