#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvv vector<vvl>
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvvl(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define rep(c, a, b) for(ll c=a;c<b;c++)
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef int ll;
typedef long double ld;
using namespace std;

//-------------------------------------------------------------------
struct segtree{
  ll M=1;
  vector<ll> dat;
  segtree(ll N, ll num){
    while(M<N) M*=2;
    for(int i=0;i<M*2-1;i++)  dat.push_back(num);
  }
  void update(ll x, ll k, ll l=0, int r=-1){
    if(r==-1) r = M;
    k+=M-1;
    dat[k] += x;
    while(k>0) k = (k-1)/2, dat[k] += x;
  }
  ll query(ll a, ll b=-1, ll k=0, ll l=0, ll r=-1){
    if(r==-1) r = M;
    if(b==-1) b = M;
    if(r<=a || b<=l) return 0;
    if(a<=l && r<=b) return dat[k];
    ll A = query(a, b, k*2+1, l, (l+r)/2);
    ll B = query(a, b, k*2+2, (l+r)/2, r);
    return A + B;
  }
};
deque<pair<char, ll>> staccum(string st){
  char now = '?';
  deque<pair<char, ll>> ret;
  ll c = 0;
  for(int i=0;i<st.size();i++){
    if(now!=st[i]){
      if(now!='?') ret.push_back(make_pair(now, c));
      now = st[i], c = 1;
    }else c++;
    if(i==st.size()-1) ret.push_back(make_pair(now, c));
  }
  return ret;
}
//-------------------------------------------------------------------
int main(int argc, char const *argv[]) {
  ll n;std::cin >> n;
  vll a(n), b(n);
  re(i, n) std::cin >> a[i];
  re(i, n) std::cin >> b[i];
  ll ans = 100000000;
  for(int i=0;i<(1<<n);i++){
    ll s = 0, tmp = 0;
    bitset<18> k(i);
    re(j, n) if(k[j]) s++;
    if(s%2) continue;
    vll num(n);
    vvl z = vv(n, 3, 0, ll);
    re(j, n) {
      num[j] = (k[j]?b[j]:a[j]);
      z[j] = vll{num[j], j, 0};
    }
    segtree seg(51, 0), rev(51, 0);
    vll x(n, 0);
    for(int j=0;j<n;j++) {
      ll q = seg.query(num[j]+1, 51);
      tmp += q, x[j]+=q;
      seg.update(1, num[j]);
    }
    for(int j=n-1;j>=0;j--){
      ll q = rev.query(0, num[j]);
      x[j]+=q;
      rev.update(1, num[j]);
    }
    re(j, n) if(k[j]!=x[j]%2) z[j][2] = 1;
    sort(all(z));
    bool flag = true;
    vector<string> t(51, "");
    re(j, n) t[z[j][0]]+=+z[j][2]+'0';

    re(j, 51){
      if(t[j].size()==0) continue;
      deque<pair<char, ll>> now = staccum(t[j]);
      if(!now.empty()&&now.front().first=='0') now.pop_front();
      if(!now.empty()&&now.back().first=='0') now.pop_back();
      ll W=0, B=0;
      bool T1=true;
      while(!now.empty()){
        pair<char, ll> T = now.front();
        now.pop_front();
        if(T.first=='1'){
          W+=T.second;
          if(T1&&T.second%2) T1 = false;
          else if(!T1&&T.second%2) T1 = true;
        }else{
          if(T1) continue;
          if(T.second%2) flag = false;
          B+=T.second;
        }
      }
      if(!T1) flag = false;
      tmp += (W+B)/2 + B/2;
    }
    if(flag) ans = min(tmp, ans);
  }
  std::cout << (ans==100000000?-1:ans) << '\n';
  return 0;
}


Measured memory_bytes: 384