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, s, t ) for( register int i = s; i <= t; ++ i )
#define re register
#define int long long
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 15 + 5 ; 
const int inf = 123456789 ;
int n, a[N], b[N], col[N], w[N], Ans, st0[N], top0, st1[N], us0[N], us1[N], t0, t1, top1 ; 
struct node {
	int id, w ; 
} s[N] ;
bool cmp( node x, node y ) {
	return x.w < y.w ; 
}
void check() {
	rep( i, 1, n ) w[i] = ( col[i] == 0 ) ? a[i] : b[i] ;
	rep( i, 1, n ) s[i].id = i, s[i].w = w[i] ;
	sort( s + 1, s + n + 1, cmp ) ; t0 = t1 = top0 = top1 = 0 ;
	int bef = 1, S0 = 0, S1 = 0, ans = 0 ;
	rep( i, 1, n ) {
		if( i & 1 ) st1[++ top1] = i ;
		else st0[++ top0] = i ; 
		if( s[i].w == s[i + 1].w ) continue ;
		rep( j, bef, i ) {
			if( col[j] ) {
				if( s[j].id & 1 ) us0[++ t0] = s[j].id ;
				else us1[++ t1] = s[j].id ;
			}
			else {
				if( s[j].id & 1 ) us1[++ t1] = s[j].id ;
				else us0[++ t0] = s[j].id ;
			}
		}
		sort( us1 + 1, us1 + t1 + 1 ), sort( us0 + 1, us0 + t0 + 1 ) ;
		if( t0 != top0 || t1 != top1 ) return ;
		for( re int j = 1; j <= t1; ++ j ) ans += abs(us1[j] - st1[j]) ;
		for( re int j = 1; j <= t0; ++ j ) ans += abs(us0[j] - st0[j]) ;
		t0 = t1 = top0 = top1 = 0, bef = i + 1 ; 
	}
	Ans = min( Ans, ans ) ; 
} 
void dfs( int x ) {
	if( x == n + 1 ) {
		check() ; return ; 
	}
	col[x] = 0, dfs( x + 1 ),
	col[x] = 1, dfs( x + 1 ) ;
}
signed main()
{
	n = gi(), Ans = inf ; 
	rep( i, 1, n ) a[i] = gi() ; 
	rep( i, 1, n ) b[i] = gi() ;
	dfs( 1 ) ;
	if( Ans >= inf ) puts("-1") ;
	else cout << Ans / 2 << endl ; 
	return 0 ;
}