#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os << "[ "; 
	for(auto v:V)os << v << " "; return os << "]";
}
template<class T> ostream& operator<<(ostream &os, set<T> S){ os << "{ "; 
	for(auto s:S) os<<s<<" ";  return os<<"}"; 
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
	return os<<"("<<P.first<<","<< P.second << ")";
}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
	os<<"{ ";for(auto m:M)os<<"("<<m.F<<":"<<m.S<<")";
	return os<<"}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define I insert 
#define pb push_back
#define F first
#define S second
// #define endl "\n"
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define For(i,n) for(ll i=0;i<(ll)n;++i)
#define Rev(i,n) for(ll i=(ll)n-1;i>=0;--i)
#define Rep(i,n) for(ll i=1;i<=(ll)n;++i)
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
typedef pair<pii,ll> ppi;
typedef vector<pii> vpii;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)


const ll N = (1<<18);
const ll MAX = 55;
const ll INF = 1e16;


ll dp[MAX][N];
int par[MAX][N];

pii A[MAX];

int inv[MAX];

int main(){
	fio; cout<<fixed<<setprecision(25);	
	int n; cin >> n;
	for(int i = 0;i < n; ++i){
		cin >> A[i].F;
	}
	for(int i = 0;i < n ; ++i)
		cin >> A[i].S;

	for(int i = 0;i < (1<<n) ; ++i){
		for(int j = 0;j < MAX ; ++j)
			dp[j][i] = INF;
	}

	for(int i = 0;i < MAX ; ++i)
		dp[i][0] = 0;
	for(int i = 0;i < (1<<n) ; ++i){

		int no = 0;
		for(int z = 0; z < 20 ; ++z){
			if(i&(1<<z))
				++no;
		}
		int kk = 0;
		for(int j = 0;j < n ; ++j){

			if( (i&(1<<j)) == 0){
				if( (no%2) == (j%2)){
					ll x = dp[A[j].F][i];
					
					int p = (i^(1<<j));
					for(int k = A[j].F ; k < MAX ; ++k){
						ll y = dp[k][p];
						if(y > x + no-kk){
							par[k][p] = j;
                          dp[k][p] = x + no-kk;
						}
						
					}
				}
				else{
					ll x = dp[A[j].S][i];
					
					int p = (i^(1<<j));
					for(int k = A[j].S ; k < MAX ; ++k){
						ll y = dp[k][p];
						if(y > x + no-kk){
							par[k][p] = j;
                           dp[k][p] = x + no-kk;
						}
						
					}
				}
			}
			else{
				++kk;
			}
		}
	}
	ll y = dp[MAX-1][(1<<n)-1];
	if(y > N*N){
		cout << -1 << endl;
		return 0;
	}
	int z = (1<<n)-1;
	int k = MAX-1;
	int idx = n;
	int p = 0;
	while(z > 0){
	
		inv[--idx] = par[k][z];
		z = (z^(1<<inv[idx]));
		if( (idx%2) ==  (inv[idx]%2) ){
			k = A[inv[idx]].F;
		} 
		else{
			k = A[inv[idx]].S;
		}
	}
	int cnt = 0;
	for(int i = 0;i < n ; ++i){
		for(int j = i+1;j < n ; ++j)
			if(inv[j] < inv[i]) ++cnt;
	}
	cout << cnt << endl;
	// if(y < N*N){
	// 	assert((y%2) == 0);
	// 	cout << y/2 << endl;
	// }
	// else
	// 	cout << -1 << endl;









	return 0;
}


Measured memory_bytes: 167296