#include <bits/stdc++.h>
               using namespace std;
               #define tani_nachi_ke  ios_base::sync_with_stdio(false); cin.tie(NULL);
               #define M_PI 3.14159265358979323846
               #define data data_
               #define ff first
               #define ss second
               #define pb push_back
               #define ll long long
               #define ld long double


               long long const inf = 1e15;
               int const N = 19;
               int a[N],b[N];
               int n;
               long long dp[(1<<18)][51];
               long long fun(int mask,int prev)
               {
                    // cout << mask << " " << pos << " " << prev << endl;
                    int pos = __builtin_popcount(mask);
                    if(pos == n)
                         return 0;
                    else if(dp[mask][prev]!=-1)
                         return dp[mask][prev];
                    else
                    {
                    // cout << mask << " " << pos << " " << prev << endl;

                         int i = 0;
                         long long ans = inf;
                         int suff[n+2] = {0};
                         while(i < n)
                         {
                              if(mask&(1<<i))
                                   suff[i]++;
                              i++;
                         }
                         i = 0;
                         // cout << "ad" << endl;
                         for(int j = n-1; j >= 0; j--)
                              suff[j] += suff[j+1];
                         while(i < n)
                         {

                              // cout << i << endl;
                              if(mask&(1<<i))
                              {
                                   i++;
                                   continue;
                              }
                              int dist = abs(i-pos);
                              // cout << dist << endl;
                              if(dist&1)
                              {
                                   if(b[i] >= prev)
                                        ans = min(ans,fun(mask|(1<<i),b[i])+suff[i]);
                              }
                              else
                              {
                                   if(a[i] >= prev)
                                        ans = min(ans,fun(mask|(1<<i),a[i])+suff[i]);
                              }
                         i++;
                         }
                    // cout << mask << " " << pos << " " << prev << " " << ans << endl;

                         return dp[mask][prev] = ans;
                    }
               }
               int main()
               {
                    tani_nachi_ke  

               
                    cin >> n;
                    for(int i = 0; i < n; i++)
                         cin >> a[i];
                    for(int i = 0; i < n; i++)
                         cin >> b[i];
                    memset(dp,-1,sizeof(dp));
                    long long ans = fun(0,0);
                    if(ans < inf)
                         cout << ans << endl;
                    else
                         cout << -1 << endl;



              
                    return 0;
               }

Measured memory_bytes: 104704