#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
//#define int long long
#define pb push_back
#define x first
#define ld long double
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) a.begin(),a.end()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int N = 51, INF = 1e9;
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, ans = INF;
    cin >> n;
    vector <int> a(n), b(n);
    for (auto &i : a) cin >> i;
    for (auto &i : b) cin >> i;
    int cnt = (n + 1) / 2;
    for (int i = 0; i < (1 << n); i++) {
        int c = 0;
        vector <pair<int, int> > A;
        for (int j = 0; j < n; j++) {
            if (j & 1) {
                if (i & (1 << j)) {
                    A.pb({b[j], 0});
                    ++c;
                }
                else {
                    A.pb({a[j], 1});
                }
            }
            else {
                if (i & (1 << j)) {
                    ++c;
                    A.pb({a[j], 0});
                }
                else {
                    A.pb({b[j], 1});
                }
            }
        }
        if (cnt != c) continue;
        int res = 0;
        int kol[N][2] = {};
        for (int j = 0; j < n; j++) {
            ++kol[A[j].x][A[j].y];
        }
        int cur = 0;
        for (int i = 0; i < n; ) {
            while (kol[cur][0] + kol[cur][1] > 0) {
                int to = i & 1;
                if (!kol[cur][to]) {
                    res = INF;
                    i = INF;
                    break;
                }
                --kol[cur][to];
                int j = i;
                for (; j < n; ) {
                    if (A[j].x == cur && A[j].y == to) {
                        break;
                    }
                    ++j;
                }
                for (int i1 = j - 1; i1 >= i; i1--) {
                    ++res;
                    swap(A[i1], A[i1 + 1]);
                }
                ++i;
            }
            ++cur;
        }
//        cout << i << ' ' << res << '\n';
        ans = min(ans, res);
    }
    if (ans >= INF) {
        cout << "-1\n";
    }
    else cout << ans << '\n';
}


Measured memory_bytes: 384