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: Time Limit Exceeded)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

//#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

    template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

int calc(const VI& x){
    int res = 0;
    int sz = SZ(x);
    FOR(i,0,sz) FOR(j,i+1,sz){
        if(x[i] > x[j]) res++;
    }
    return res;
}


signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    int N; cin >> N;
    VI A(N),B(N);
    REP(i,N) cin >> A[i];
    REP(i,N) cin >> B[i];
    int ans = INF;
    for(int mask = 0; mask < (1 << N); mask++){
        vector<int> sorted;
        vector<int> origin;
        vector<PII> need(51);
        bool ok = true;
        REP(i,N){
            if((mask >> i)&1) {
                origin.eb(B[i]);
                if(i%2 == 0){
                    need[B[i]].second++;
                }else{
                    need[B[i]].first++;
                }
            }
            else {
                origin.eb(A[i]);
                if(i%2 == 0){
                    need[A[i]].first++;
                }else{
                    need[A[i]].second++;
                }
            }
        }

        sorted = origin;
        sort(sorted.begin(), sorted.end());
        vector<queue<int>> ord(110);
        vector<PII> vp(51);
        REP(i,N){
            int x = sorted[i];
            if(i%2 == 0){
                ord[x*2].push(i);
                vp[x].first++;
            }else{
                ord[x*2+1].push(i);
                vp[x].second++;
            }
        }
        if(need != vp) continue;


        //REP(i,N){
        //    cout << origin[i] << " ";
        //}
        //cout << endl;

        vector<int> target;
        REP(i,N){
            int x = origin[i];
            if(((mask >> i)&1) == 0){
                if(i%2 == 0){
                    if(ord[x*2].empty()){
                        ok = false;
                        break;
                    }
                    target.eb(ord[x*2].front());
                    ord[x*2].pop();
                }else{
                    if(ord[x*2+1].empty()){
                        ok = false;
                        break;
                    }
                    target.eb(ord[x*2+1].front());
                    ord[x*2+1].pop();
                }
            }else{
                if(i%2 == 1){
                    if(ord[x*2].empty()){
                        ok = false;
                        break;
                    }
                    target.eb(ord[x*2].front());
                    ord[x*2].pop();
                }else{
                    if(ord[x*2+1].empty()){
                        ok = false;
                        break;
                    }
                    target.eb(ord[x*2+1].front());
                    ord[x*2+1].pop();
                }
            }
        }

        if(!ok) continue;
        chmin(ans,calc(target));


    }
    if(ans == INF) cout << -1 << endl;
    else cout << ans << endl;

}