#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define lll __int128
#define ull unsigned long long
#define fi first
#define se second
#define db double
#define ld long double
#define lld __float128

/// order_of_key, find_by_order

template<class T, class COMP>
using custom_set = tree<T, null_type, COMP, rb_tree_tag, tree_order_statistics_node_update>;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T, class U>
using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rnd_64(chrono::steady_clock::now().time_since_epoch().count());

int dp[(1 << 18)][51];

vector<int> msk[19];

int main()
{
    int n; cin >> n;
    pair<int, int> dat[n];
    for(int i = 0; i < n; i++)
    {
        cin >> dat[i].fi;
    }
    for(int i = 0; i < n; i++)
    {
        cin >> dat[i].se;
    }
    for(int i = 0; i < (1 << n); i++)
    {
        for(int j = 0; j <= 50; j++)
        {
            dp[i][j] = 1e9;
        }
        int sm = 0;
        for(int j = 0; j < n; j++)
        {
            if(i & (1 << j))
                sm++;
        }
        msk[sm].push_back(i);
    }
    dp[0][0] = 0;
    for(int len = 0; len < n; len++)
    {
        for(int mask : msk[len])
        {
//            cout << mask << ' ';
            for(int pr = 0; pr <= 50; pr++)
            {
                int sm = 0;
                for(int j = 0; j < n; j++)
                {
                    if(mask & (1 << j))
                    {
                        sm++; continue;
                    }
                    int dist = j-sm;
                    int x;
                    if(abs(j-len) % 2 == 0)
                        x = dat[j].fi;
                    else
                        x = dat[j].se;
                    if(x >= pr)
                    {
//                        cout << mask << " > " << j << " > " << (mask | (1 << j)) << '\n';
                        dp[mask | (1 << j)][x] = min(dp[mask | (1 << j)][x], dp[mask][pr] + dist);
                    }
                }
            }
        }
//        cout << '\n';
    }
    int ans = 1e9;
    for(int i = 0; i <= 50; i++)
    {
        ans = min(ans, dp[(1 << n)-1][i]);
//        cout << dp[(1 << n)-1][i] << ' ';
    }
//    for(int i = 0; i < (1 << n); i++)
//    {
//        string s = ""; int x = i;
//        for(int j = 0; j < n; j++)
//        {
//            s += (x % 2)+'0';
//            x /= 2;
//        }
////        reverse(s.begin(), s.end());
//        cout << s << ": ";
//        for(int j = 0; j <= 50; j++)
//        {
//            if(dp[i][j] == 1e9) cout << "-1 ";
//            else cout << dp[i][j] << ' ';
//        } cout << '\n';
//    }
    if(ans == 1e9)
        ans = -1;
    cout << ans << endl;
}






































/// shche ne vmerla Ykraina


Measured memory_bytes: 53748