#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
int A[20], B[20];
int C[20];
int cnt[20];
int cnt_same[55];
int cnt_inv[55];
vector<int> odd[55];
vector<int> even[55];
P p[20];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N; i++){
        cin >> A[i];
    }
    for(int i = 0; i < N; i++){
        cin >> B[i];
    }
    int ans = 1000000;
    bool find = false;
    for(int i = 0; i < (1<<N); i++){
        int tmp = 0;
        for(int j = 0; j <= 50; j++) {
            cnt_same[j] = 0;
            cnt_inv[j] = 0;
            odd[j].clear();
            even[j].clear();
        }
        for(int j = 0; j < N; j++){
            if(i&(1<<j)) C[j] = B[j];
            else C[j] = A[j];
            p[j] = P(C[j], j);
        }
        sort(p, p+N);
        for(int j = 0; j < N; j++) {
            cnt[j] = 0;
        }
        for(int j = 0; j < N; j++){
            for(int k = j+1; k < N; k++){
                if(p[j].second > p[k].second) {
                    tmp++;
                }
            }
        }

        for(int j = 0; j < N; j++){
            if(i&(1<<j)){
                if((j+1)%2 == 0) odd[C[j]].push_back(j);
                else even[C[j]].push_back(j);
            }else{
                if((j+1)%2 == 1) odd[C[j]].push_back(j);
                else even[C[j]].push_back(j);
            }
        }
        
        bool ok = true;
        int cur = 0;
        int pos = 0;
        for(int j = 1; j <= 50; j++){
            vector<int> buf;
            if(pos%2 == 0){
                if(odd[j].size() == even[j].size() || odd[j].size() == even[j].size()+1){
                    int s = even[j].size();
                    for(int k = 0; k < s; k++){
                        buf.push_back(odd[j][k]);
                        buf.push_back(even[j][k]);
                    }
                    if(odd[j].size() == even[j].size()+1) buf.push_back(odd[j][s]);
                }else{
                    ok = false;
                }
            }else{
                if(odd[j].size() == even[j].size() || odd[j].size()+1 == even[j].size()){
                    int s = odd[j].size();
                    for(int k = 0; k < s; k++){
                        buf.push_back(even[j][k]);
                        buf.push_back(odd[j][k]);
                    }
                    if(odd[j].size()+1 == even[j].size()) buf.push_back(even[j][s]);
                }else{
                    ok = false;
                }
            }
            pos += (odd[j].size() + even[j].size());
            if(ok){
                for(int k = 0; k < buf.size(); k++){
                    for(int l = k+1; l < buf.size(); l++){
                        if(buf[k] > buf[l]) cur++;
                    }
                }
            }
        }
        tmp += cur;
        if(ok) {
            find = true;
            ans = min(ans, tmp);
        }
    }
    if(!find){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
}

Measured memory_bytes: 384