#include <bits/stdc++.h>
using namespace std;
#define R(i,n) for(int i=0;i<n;i++)

signed main(){
    int n,a=1e9,c[2][20];
    cin>>n;
    R(i,n)cin>>c[0][i];
    R(i,n)cin>>c[1][i];
    vector<vector<int>> p(1<<n,vector<int>(51,1e9));
    p[0][0]=0;
    R(B,1<<n){
        int o=0,d[n];
        R(i,n)if(!(B&1<<i))d[i]=o++;
        R(i,n){
            int k=abs(i-n+o);
            R(j,c[k%2][i]+1)p[B|1<<i][c[k%2][i]]=min(p[B|1<<i][c[k%2][i]],p[B][j]+d[i]);
        }
    }
    R(i,51)a=min(a,p[(1<<n)-1][i]);
    cout<<(a==1e9?-1:a);
}