Problem:
Score : 700 points

Problem Statement

We have N cards numbered 1, 2, ..., N.
Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side.
Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.

Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.

Choose an integer i (1 \leq i \leq N - 1).
Swap the i-th and (i+1)-th cards from the left, then flip these two cards.

Constraints

1 \leq N \leq 18

1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If it is impossible to have a non-decreasing sequence, print -1.
If it is possible, print the minimum number of operations required to achieve it.

Sample Input 1

3
3 4 3
3 2 3

Sample Output 1

1

By doing the operation once with i = 1, we have a sequence [2, 3, 3] facing up, which is non-decreasing.

Sample Input 2

2
2 1
1 2

Sample Output 2

-1

After any number of operations, we have the sequence [2, 1] facing up, which is not non-decreasing.

Sample Input 3

4
1 2 3 4
5 6 7 8

Sample Output 3

0

No operation may be required.

Sample Input 4

5
28 15 22 43 31
20 22 43 33 32

Sample Output 4

-1

Sample Input 5

5
4 46 6 38 43
33 15 18 27 37

Sample Output 5

3

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; typedef long long ll; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

int main() {
    int N;
    cin >> N;
    vector<int> A(2 * N);
    rep(i, 2 * N) cin >> A[i];

    vector<int> num(N);
    vector<int> idx(N);
    iota(all(idx), 0);
    int ans = INF;
    rep(bit, 0, 1 << N) {
        bool ok = true;
        rep(i, N) ok ^= bit >> i & 1;
        if (!ok) continue;

        rep(i, N) num[i] = A[i + N * (bit >> i & 1)];
        stable_sort(all(idx), [&](int l, int r) { return num[l] < num[r]; });

        vector<int> eo[2];
        for (int i = 0, j; i < N; i = j) {
            for (j = i + 1; j < N; ++j) {
                if (num[idx[j]] != num[idx[i]]) break;
            }

            if (j - i == 1) {
                if ((abs(i - idx[i]) & 1) ^ (bit >> idx[i] & 1)) {
                    ok = false;
                    break;
                }
            }
            else {
                eo[0].resize(0);
                eo[1].resize(0);
                rep(k, i, j) {
                    eo[(abs(i - idx[k]) & 1) ^ (bit >> idx[k] & 1)].push_back(idx[k]);
                }
                if ((int)eo[0].size() - (int)eo[1].size() != (j - i) % 2) {
                    ok = false;
                    break;
                }

                int ke = 0, ko = 0;
                rep(k, i, j) {
                    if ((k ^ i) & 1) {
                        idx[k] = eo[1][ko];
                        ko++;
                    }
                    else {
                        idx[k] = eo[0][ke];
                        ke++;
                    }
                }
            }
        }
        if (!ok) continue;

        int move = 0;
        rep(i, N - 1) {
            int j = 0;
            for (j = i; j < N; ++j) {
                if (idx[j] == i) break;
            }
            for (; j != i; --j) {
                swap(idx[j], idx[j - 1]);
                move++;
            }
        }

        ans = min(ans, move);
    }

    write(ans == INF ? -1 : ans);
}