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>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define mod (int)(1e9+7)
#define INF (ll)(1e18)
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }


void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int T;
    // cin >> T;
    T = 1;
    
    while (T--) {
        solve();
    }
}


template<typename T>
struct BIT {
    vector<T> array;
    const int n;
public:
    BIT(int _n) : array(_n + 1, 0), n(_n) {}
    
    // 1番目からi番目までの累積和
    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += array[i];
            i -= i & -i;
        }
        return s;
    }
    
    // [i, j]の要素の総和
    T sum(int i, int j) {
        T ret_i = sum(i - 1);
        T ret_j = sum(j);
        return ret_j - ret_i;
    }
    
    void add(int i, T x) {
        while (i <= n) {
            array[i] += x;
            i += i & -i;
        }
    }
};


void solve() {
    int n;
    cin >> n;
    vector<pair<pii, int> > a(n);
    rep(i, n) {
        cin >> a[i].F.F;
        a[i].S = i;
    }
    rep(i, n) cin >> a[i].F.S;
    
    int ans = inf;
    rep(i, 1 << n) {
        vector<pii> b(n);
        rep(j, n) b[j] = pii(a[j].F.F, j);
        rep(j, n) {
            if (i & (1 << j)) {
                b[j].F = a[j].F.S;
            }
        }
        // cout << endl; rep(j, n) cout << b[j].S << " "; cout << endl;
        // rep(j, n) cout << b[j].F << " "; cout << endl;
        
        sort(all(b));
        int flg = 0;
        rep(j, n) {
            if ((b[j].S - j) % 2 && b[j].F != a[b[j].S].F.S) { flg = 1; break; }
            if ((b[j].S - j) % 2 == 0 && b[j].F != a[b[j].S].F.F) { flg = 1; break; }
            if (j && b[j].F < b[j - 1].F) { flg = 1; break; }
        }
        if (flg) continue;
        
        // cout << endl; rep(j, n) cout << b[j].S << " "; cout << endl;
        // rep(j, n) cout << b[j].F << " "; cout << endl;
        
        BIT<int> bit(n);
        int sum = 0;
        for (int j = n - 1; j >= 0; --j) {
            sum += bit.sum(b[j].S);
            bit.add(b[j].S + 1, 1);
        }
        chmin(ans, sum);
    }
    cout << (ans == inf ? -1 : ans) << endl;
}