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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.*;

public class Main{
	public static void main(String[] args){
		System.err.println();
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		int[][] a=new int[2][n];
		for(int i=0;i<n;++i)
			a[i%2][i]=s.nextInt();
		for(int i=0;i<n;++i)
			a[i%2^1][i]=s.nextInt();

		System.err.println(Arrays.toString(a[0]));
		System.err.println(Arrays.toString(a[1]));
		System.err.println();

		int r=Integer.MAX_VALUE;
		loop:
		for(int[]p:new Permutation(n)) {
			for(int i=0;i<n-1;++i) {
				if(a[i%2][p[i]]>a[i%2^1][p[i+1]])
					continue loop;
			}
			System.err.println(Arrays.toString(p));
			r=Math.min(r,inv(Arrays.copyOf(p,n)));
		}
		System.out.println(r==Integer.MAX_VALUE?-1:r);
	}

	private static void swap(int[] a,int i){
		int t=a[i];
		a[i]=a[i+1];
		a[i+1]=t;
	}

	static int inv(int[]a) {
		int r=0,n=a.length;
		for(int i=0;i<n;++i) {
			for(int j=n-1;j>i;--j) {
				if(a[j-1]>a[j]) {
					swap(a,j-1);
					++r;
				}
			}
		}
		return r;
	}
}


class Permutation implements Iterable<int[]>{
	private final int size;

	/**
	 * <p>
	 * 順列を表すクラスのコンストラクタ。反復子が返す配列の要素数を指定する。
	 * @param size
	 * 順列の要素数（10程度が妥当な最大値）
	 * @throws IllegalArgumentException
	 * 引数（順列の要素数）が0以下の場合
	 */
	public Permutation(int size){
		if(size<=0) throw new IllegalArgumentException();
		this.size=size;
	}

	@Override
	public Iterator<int[]> iterator(){
		return new Iter(size);
	}

	private class Iter implements Iterator<int[]>{
		private final int[]	source;
		private boolean		isFirst	=true;

		private Iter(int size){
			source=new int[size];
			for(int i=0;i<size;++i){
				source[i]=i;
			}
		}

		/**
		 * <p>
		 * 反復子がまだ順列を返しうるか調べる。
		 * 本メソッドは{@link Iter#next()}呼び出し前に必ず1回ずつ実行する必要がある。
		 * @return 順列が存在する場合はtrue
		 */
		@Override
		public boolean hasNext(){
			if(isFirst){
				isFirst=false;
				return true;
			}

			int n=source.length;
			for(int i=n-1;i>0;i--){
				if(source[i-1]<source[i]){
					int j=n;
					while(source[i-1]>=source[--j])
						;
					swap(source,i-1,j);
					reverse(source,i,n);
					return true;
				}
			}
			reverse(source,0,n);
			return false;
		}

		/**
		 * <p>
		 * 次の順列を表すint型配列を返す。
		 * <p>
		 * このメソッドが返す参照は常に同じ配列に対する参照である。
		 * このため配列の要素を変更したり次回の{@link Iter#next()}呼び出し後も参照する場合は、
		 * クラスの呼び出し側が配列のクローンを生成して利用する必要がある。
		 * @return 順列を表すint型配列
		 */
		@Override
		public int[] next(){
			return source;
		}

		@Override
		public void remove(){
			throw new UnsupportedOperationException();
		}

		private void swap(int[] is,int i,int j){
			int t=is[i];
			is[i]=is[j];
			is[j]=t;
		}

		private void reverse(int[] is,int s,int t){
			while(s<--t)
				swap(is,s++,t);
		}
	}
}