Problem:
Score : 700 points

Problem Statement

We have N cards numbered 1, 2, ..., N.
Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side.
Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.

Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.

Choose an integer i (1 \leq i \leq N - 1).
Swap the i-th and (i+1)-th cards from the left, then flip these two cards.

Constraints

1 \leq N \leq 18

1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If it is impossible to have a non-decreasing sequence, print -1.
If it is possible, print the minimum number of operations required to achieve it.

Sample Input 1

3
3 4 3
3 2 3

Sample Output 1

1

By doing the operation once with i = 1, we have a sequence [2, 3, 3] facing up, which is non-decreasing.

Sample Input 2

2
2 1
1 2

Sample Output 2

-1

After any number of operations, we have the sequence [2, 1] facing up, which is not non-decreasing.

Sample Input 3

4
1 2 3 4
5 6 7 8

Sample Output 3

0

No operation may be required.

Sample Input 4

5
28 15 22 43 31
20 22 43 33 32

Sample Output 4

-1

Sample Input 5

5
4 46 6 38 43
33 15 18 27 37

Sample Output 5

3

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
	if (v.empty())return out;
	constexpr bool is_vector_v = is_vector<T>::value;
	if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
	else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
	return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

vector<int> demical(ll n, int k, int l = -1) {
	vector<int> rtn;
	if (l < 0) {
		while (n > 0) {
			rtn.push_back(n % k);
		}
	} else {
		rtn.resize(l);
		REP(i, l) {
			rtn[i] = n % k;
			n /= k;
		}
	}
	return rtn;
}

template <typename T>
vector<pair<int, T>> rle(vector<T> a)
{
	int n = (int)a.size();
	vector<pair<int, T>> res;
	if (n == 0)
	{
		return res;
	}
	T now = a[0];
	int con = 0;
	for (int i = 0; i < n; ++i)
	{
		if (a[i] == now)
		{
			con++;
		} else
		{
			res.push_back(pair<int, T>(con, now));
			now = a[i];
			con = 1;
		}
	}
	res.push_back(pair<int, T>(con, now));
	return res;
}


int main() {
	IOS;
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	REP(i, n) {
		cin >> a[i];
	}
	REP(i, n) {
		cin >> b[i];
	}
	int t = 1 << n;
	int res = INT_MAX;
	REP(i, t) {
		auto d = demical(i, 2, n);
		vector<P> p(n);
		REP(j, n) {
			if (d[j] == 1) {
				p[j].first = a[j];
			} else {
				p[j].first = b[j];
			}
			p[j].second = j;
		}
		sort(all(p));
		vector<int> q(n);
		REP(j, n) {
			q[j] = p[j].first;
		}
		auto rl = rle(q);
		int tmp = 0;
		bool flag = true;
		vector<int> f(n, 1);
		vector<int> co(n, 0);
		REP(j, n) {
			REP(k, n) {
				if (k<j && p[k].second>p[j].second) {
					co[j]++;
					tmp++;
				} else if (j<k && p[j].second>p[k].second) {
					co[j]++;
					tmp++;
				}/*else if (k<j&& p[k].second==p[j].second) {
					co[j]++;
					tmp++;
				}*/
			}
		}
		int inc = 0;
		REP(j, n) {
			if (co[j] % 2 == 0 && d[j] == 1) {

			} else if (co[j] % 2 == 1 && d[j] == 0) {

			} else if(j<n-1&&p[j].second==p[j+1].second&&f[j]==1){
				inc++;
				f[j] = f[j + 1] = 0;
			} else {
				flag = false;
			}
		}
		if (flag) {
			chmin(res, tmp/2+inc);
		}
	}
	if (res == INT_MAX) {
		put(-1);
	} else {
		put(res);
	}
	return 0;
}