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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;
using static BinaryRepresentation;

public class D
{
	public static void Main() => (new Solver()).Solve();
}

public class Solver
{
	public void Solve()
	{
		int N = GetInt();
		int[] A = GetInts(N);
		int[] B = GetInts(N);

		int ans = int.MaxValue;
		int comb = (1 << (N / 2)) - 1;
		int end = (1 << N);
		while(comb < end)
		{
			var total = new List<int>();
			var even = new List<int>();
			var odd = new List<int>();
			for(int i = 0; i < N; i++)
			{
				if(contains(comb, i))
				{
					odd.Add((i % 2 == 1) ? A[i] : B[i]);
					total.Add((i % 2 == 1) ? A[i] : B[i]);
				}
				else
				{
					even.Add((i % 2 == 0) ? A[i] : B[i]);
					total.Add((i % 2 == 0) ? A[i] : B[i]);
				}
			}
			odd.Sort();
			even.Sort();

			var merge = new int[N];
			for(int i = 0; i < even.Count; i++)
				merge[i * 2] = even[i];

			for(int i = 0; i < odd.Count; i++)
				merge[i * 2 + 1] = odd[i];

			bool valid = true;
			for(int i = 0; i < N - 1; i++)
			{
				if(A[i] > A[i + 1])
				{
					valid = false;
					break;
				}
			}

			if(valid)
				ans = Min(ans, calc(N, total));

			comb = nextCombination(comb);
		}

		WriteLine((ans == int.MaxValue) ? -1 : ans);
	}

	private bool contains(int s, int x) => ((s >> x) & 1) == 1;

	private int calc(int N, List<int> a)
	{
		int res = 0;
		var bit = new BinaryIndexedTree<int>(51, 0, (x, y) => x + y);
		for(int j = 0; j < N; j++)
		{
			res += j - bit.Query(a[j]);
			bit.Update(a[j], 1);
		}
		return res;
	}
}

public static class BinaryRepresentation
{
	/*
	* initialize: array = { 0, 1, 2, ..., N - 1 } 
	* do { ... } while(nextPermutaion(ref array));
	*/
	private static bool nextPermutation(ref int[] array)
	{
		int len = array.Length;
		int i = len - 2;
		while((i >= 0) && (array[i] > array[i + 1]))
			i--;
		if(i < 0)
			return false;

		int pivot = array[i];
		int j = i + 1;
		while((j < len - 1) && (array[j + 1] > pivot))
			j++;
		
		int tmp = array[i];
		array[i] = array[j];
		array[j] = tmp;
		
		Array.Reverse(array, i + 1, len - i - 1);

		return true;
	}

	/* 
	* nCk
	* initialize: comb = (1 << k) - 1
	* while(comb < (1 << n)) { ...; comb = nextCombination(comb); };
	*/
	public static int nextCombination(int comb)
	{
		int x = comb & -comb;
		int y = comb + x;
		return (((comb & ~y) /x) >> 1) | y;
	}

	public static int popcnt(int v) {
		v = v - ((v >> 1) & 0x55555555);
		v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
		return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
	}

	public static long popcnt(long v) {
		v = v - ((v >> 1) & 0x5555555555555555);
		v = (v & 0x3333333333333333) + ((v >> 2) & 0x3333333333333333);
		return ((v + (v >> 4) & 0xF0F0F0F0F0F0F0F) * 0x101010101010101) >> 56;
	}
}

public class BinaryIndexedTree<T>
{
	private int N;
	private T Identity;
	private Func<T, T, T> Func;
	private T[] bit;

	public BinaryIndexedTree(int n, T identity, Func<T, T, T> func)
	{
		N = n;
		Identity = identity;
		Func = func;
			
		bit = new T[N + 1];  // 1-index
		for(int i = 0; i <= N; i++)
			bit[i] = Identity;
	}

	public void Update(int i, T x)
	{
		while(i <= N)
		{
			bit[i] = Func(bit[i], x);
			i += i & -i;
		}
	}

	public T Query(int i)
	{
		T s = Identity;
		while(i > 0)
		{
			s = Func(s, bit[i]);
			i -= i & -i;
		}
		return s;
	}

	/* bit must be monotonically increasing */
	public int LowerBound(T key, Comparison<T> comp, Func<T, T, T> inv)
	{
		if(comp(key, Identity) <= 0)
			return 0;

		int pow = 1;
		while(pow <= N)
			pow <<= 1;
		pow >>= 1;

		int x = 0;
		for(int k = pow; k > 0; k >>= 1)
		{
			if((x + k <= N) && (comp(bit[x + k], key) < 0))
			{
				key = inv(key, bit[x + k]);
				x += k;
			}
		}

		return x + 1;
	}
}

public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => Enumerable.Range(0, (int)N).Select(_ => GetInt()).ToArray();
	public static long[] GetLongs(long N) => Enumerable.Range(0, (int)N).Select(_ => GetLong()).ToArray();
	public static double[] GetDoubles(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDouble()).ToArray();
	public static decimal[] GetDecimals(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDecimal()).ToArray();
	public static string[] GetStrings(long N) => Enumerable.Range(0, (int)N).Select(_ => GetString()).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) < 0)
			x = y;
	}

	public static void chmin<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) > 0)
			x = y;
	}

	public static void swap<T>(ref T x, ref T y)
	{
		T tmp = x;
		x = y;
		y = tmp;
	}
}