#include <iostream>
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define BITT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define INF BITT(29)
#define pb push_back
#define mod 1000000007

using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

#define FACSIZE 200002

ll invfac[FACSIZE];
ll fac[FACSIZE];

ll gcd(ll, ll);
ll powLL(ll x, ll y);

ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
ll comb(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void fact_init();

template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return 1;
  }
  return 0;
}

ll n, N;
vector<ll> A;
vector<ll> B;

int solve()
{
  using pint = pair<int, int>;
  vector<vector<int>> dp((1 << N) + 1, vector<int>(55, INF));
  dp[0][0] = 0;
  for (int bit = 0; bit < (1 << N); ++bit)
  {
    int con = 0;
    for (int i = 0; i < N; ++i)
      if (bit & (1 << i))
        ++con;

    // bit に含まれない残りの要素が何番目に対応するか
    vector<pint> ords;
    int iter = con;
    for (int i = 0; i < N; ++i)
      if (!(bit & (1 << i)))
        ords.emplace_back(i, iter++);

    for (int s = 0; s <= 50; ++s)
    {
      if (dp[bit][s] >= INF)
        continue;
      for (auto p : ords)
      {
        // p.first: もともとの a の index
        // p.second: dp[bit][s] の状態での index
        int ns = -1;
        if (abs(p.first - con) % 2 == 0)
          ns = A[p.first];
        else
          ns = B[p.first];

        if (ns >= s)
        {
          chmin(dp[bit | (1 << p.first)][ns], dp[bit][s] + abs(p.second - con));
        }
      }
    }
  }
  int res = INF;
  for (int s = 0; s <= 50; ++s)
    chmin(res, dp[(1 << N) - 1][s]);
  if (res < INF)
    return res;
  else
    return -1;
}

int main(void)
{
  cin >> n;

  N = n;

  A.resize(n);
  B.resize(n);

  //rep(i, n) cin >> A[i] >> B[i];
  rep(i, n) cin >> A[i];
  rep(i, n) cin >> B[i];

  //cout << solve() << endl;

  //return 0;
  ///*
  using pa = pair<ll, ll>;

  vector<vector<ll>> dp((1 << 18), vector<ll>(51, INT_MAX));

  dp[0][0] = 0;
  vector<pa> notSelected;
  for (ll i = 0; i < (1 << n); i++)
  {
    notSelected.clear();
    ll cnt = 0;
    for (ll j = 0; j < n; j++)
    {
      if (!((1 << j) & i))
      {
        notSelected.push_back(make_pair(j, cnt));
        cnt++;
      }
    }
    ll numSel = n - notSelected.size();
    //cout << i << "," << numSel << endl;
    for (ll j = 0; j <= 50; j++)
    {
      for (auto p : notSelected)
      {
        ll idx = p.first;
        ll val = ((numSel + idx) % 2) == 0 ? A[idx] : B[idx];
        if (val >= j)
          dp[i | (1 << idx)][val] = min(dp[i][j] + abs(p.second), dp[i | (1 << idx)][val]);
      }
    }
  }
  ll ans = INT_MAX;
  for (ll i = 1; i <= 50; i++)
  {
    ans = min(ans, dp[(1 << n) - 1][i]);
  }
  if (ans == INT_MAX)
  {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;

  return 0;
  //*/
}


Measured memory_bytes: 114944