#include <iostream>
#include <vector>

using namespace std;

struct ele
{
    int x, y, nr;
};

const int INF = 1000000000;
int dp[1 << 18][18][2];

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector<ele> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i].x;
    for (int i = 0; i < n; ++i)
        cin >> a[i].y;
    for (int i = 0; i < n; ++i)
        a[i].nr = i;
    if (n == 1)
    {
        cout << "0";
        return 0;
    }
    for (int i = 0; i < (1 << 18); ++i)
        for (int j = 0; j < 18; ++j)
            for (int k = 0; k < 2; ++k)
                dp[i][j][k] = INF;
    for (int i = 0; i < n; ++i)
        dp[1 << i][i][i % 2] = i;
    for (int mask = 1; mask < (1 << n) - 1; ++mask)
    {
        vector<int> r;
        for (int i = 0; i < n; ++i)
            if ((mask & (1 << i)) == 0)
                r.push_back(i);
        vector<int> p(r.size(), 0);
        for (int i = 0; i < r.size(); ++i)
            for (int j = 0; j < n; ++j)
                if ((mask & (1 << j)) > 0 && j > r[i])
                    p[i] ^= 1;
        for (int i = 0; i < n; ++i)
        {
            if ((mask & (1 << i)) == 0)
                continue;
            for (int j = 0; j < 2; ++j)
            {
                if (dp[mask][i][j] == INF)
                    continue;
                for (int k = 0; k < r.size(); ++k)
                {
                    if (k % 2 == 0)
                    {
                        if (p[k] == 0)
                        {
                            if (j == 0)
                            {
                                if (a[i].x <= a[r[k]].x)
                                    dp[mask | (1 << r[k])][r[k]][0] = min(dp[mask | (1 << r[k])][r[k]][0], dp[mask][i][j] + k);
                            }
                            else
                            {
                                if (a[i].y <= a[r[k]].x)
                                    dp[mask | (1 << r[k])][r[k]][0] = min(dp[mask | (1 << r[k])][r[k]][0], dp[mask][i][j] + k);
                            }
                        }
                        else
                        {
                            if (j == 0)
                            {
                                if (a[i].x <= a[r[k]].y)
                                    dp[mask | (1 << r[k])][r[k]][1] = min(dp[mask | (1 << r[k])][r[k]][1], dp[mask][i][j] + k);
                            }
                            else
                            {
                                if (a[i].y <= a[r[k]].y)
                                    dp[mask | (1 << r[k])][r[k]][1] = min(dp[mask | (1 << r[k])][r[k]][1], dp[mask][i][j] + k);
                            }
                        }
                    }
                    else
                    {
                        if (p[k] == 0)
                        {
                            if (j == 0)
                            {
                                if (a[i].x <= a[r[k]].y)
                                    dp[mask | (1 << r[k])][r[k]][1] = min(dp[mask | (1 << r[k])][r[k]][1], dp[mask][i][j] + k);
                            }
                            else
                            {
                                if (a[i].y <= a[r[k]].y)
                                    dp[mask | (1 << r[k])][r[k]][1] = min(dp[mask | (1 << r[k])][r[k]][1], dp[mask][i][j] + k);
                            }
                        }
                        else
                        {
                            if (j == 0)
                            {
                                if (a[i].x <= a[r[k]].x)
                                    dp[mask | (1 << r[k])][r[k]][0] = min(dp[mask | (1 << r[k])][r[k]][0], dp[mask][i][j] + k);
                            }
                            else
                            {
                                if (a[i].y <= a[r[k]].x)
                                    dp[mask | (1 << r[k])][r[k]][0] = min(dp[mask | (1 << r[k])][r[k]][0], dp[mask][i][j] + k);
                            }
                        }
                    }
                }
            }
        }
    }
    int odp = INF;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < 2; ++j)
            odp = min(odp, dp[(1 << n) - 1][i][j]);
    if (odp == INF)
        odp = -1;
    cout << odp;
    return 0;
}


Measured memory_bytes: 37248