#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
const ll INF = LLONG_MAX;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

ll f(ll a, ll b){return a+b;}
ll other = 0;

struct segment_tree{
    ll n;
    ll seg[1000000];
    //要素数Nのセグメント木を構築
    segment_tree(int N){
        n = 1;
        while(n < N) n *= 2;
        fill_n(seg, 2*n-1, other);
    }
    //i番目の要素をxに変更(0番目から数える)
    void update(int i, ll x){
        i += n-1;
        seg[i] = x;
        while(i > 0){
            i = (i-1)/2;
            seg[i] = f(seg[2*i+1], seg[2*i+2]);
        }
    }
    //要素数N以上の最小の２べきnを取得
    ll size(){return n;}
    //query(a, b, 0, 0, n)でa番目からb-1番目までの演算の結果を取得
    ll query(int a, int b, int i, int l, int r){
        if(a >= r || b <= l) return other;
        elif(a <= l && r <= b) return seg[i];
        else{
            int vl = query(a, b, 2*i+1, l, (l+r)/2);
            int vr = query(a, b, 2*i+2, (l+r)/2, r);
            return f(vl, vr);
        }
    }
};

int main(){
    int N;
    cin >> N;
    int A[N], B[N], e[N+1];
    ll ans = INF;
    rep(i, N) cin >> A[i];
    rep(i, N) cin >> B[i];
    e[0] = 1;
    rep2(i, 1, N) e[i] = 2*e[i-1];
    rep(i, e[N]){
        pll p[N];
        int s[N];
        rep(j, N){
            if(i%e[j+1] >= e[j]){
                s[j] = 1;
                p[j] = pll(A[j], j);
            }
            else{
                s[j] = -1;
                p[j] = pll(B[j], j);
            }
        }
        sort(p, p+N);
        int c[N];
        vector<pii> vec;
        rep(j, N){
            if(j == N-1){
                c[p[j].second] = j;
            }
            elif(p[j].first < p[j+1].first){
                c[p[j].second] = j;
            }
            else{
                int st = j, en = j;
                while(en < N-1 && p[en+1].first == p[st].first) en++;
                vec.push_back(pii(st, en));
                j = en;
            }
        }
        rep(j, vec.size()){
            int st = vec[j].first, en = vec[j].second;
            priority_queue<int, vector<int>, greater<int> > que1, que2;
            rep2(k, st, en){
                int l = p[k].second;
                if(l%2 == 1 && s[l] == 1) que1.push(l);
                elif(l%2 == 0 && s[l] == -1) que1.push(l);
                else que2.push(l);
            }
            int odd, even;
            if(st%2 == 1 && en%2 == 1){
                odd = (en-st)/2 + 1;
                even = (en-st)/2;
            }
            elif(st%2 == 0 && en%2 == 0){
                even = (en-st)/2 + 1;
                odd = (en-st)/2;
            }
            else{
                odd = (en-st+1)/2;
                even = (en-st+1)/2;
            }
            if(que1.size() != odd){
                rep2(k, st, en){
                    int l = p[k].second;
                    c[l] = k;
                }
            }
            else{
                rep2(k, st, en){
                    if(k%2 == 0){
                        c[que2.top()] = k;
                        que2.pop();
                    }
                    else{
                        c[que1.top()] = k;
                        que1.pop(); 
                    }
                }
            }
        }
        bool able = true;
        rep(j, N){
            int x = abs(c[j]-j);
            if(x%2 == 1 && s[j] == 1) able = false;
            elif(x%2 == 0 && s[j] == -1) able = false;
        }
        if(!able) continue;
        segment_tree seg(N);
        ll tmp = 0, n = seg.size();
        rep(j, N){
            tmp += seg.query(c[j]+1, N, 0, 0, n);
            seg.update(c[j], 1);
        }
        ans = min(ans, tmp);
    }
    if(ans == INF) cout << -1 << endl;
    else cout << ans << endl;
}

Measured memory_bytes: 2304