import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto N = RD;
	auto A = RDA(-1);
	auto B = RDA(-1);

	long ans = long.max;
	foreach (i; 0..2^^N+1)
	{
		auto cnt = popcnt(cast(uint)i);
		if (cnt % 2 == 1) continue;
		long[][] set = new long[][](50);
		auto eo = new long[](N);
		foreach (j; 0..N)
		{
			auto bit = 1L << j;
			if (i & bit)
				set[B[j]] ~= j;
			else
				set[A[j]] ~= j;

			eo[j] = i & bit ? (j+1) % 2 : j % 2;
		}

		bool ok = true;
		long l;
		auto index = new long[](N);
		auto pos = new long[](N);
		foreach (j; 0..50)
		{
			auto c = new long[](2);
			foreach (e; set[j])
				++c[eo[e]];
			
			auto p = l % 2;
			if (c[p] != (c[0]+c[1]+1) / 2)
			{
				ok = false;
				break;
			}

			long ll, rr;
			set[j].sort();
			foreach (e; set[j])
			{
				if (eo[e] == p)
				{
					auto pp = l+ll*2;
					index[e] = pp;
					pos[pp] = e;
					++ll;
				}
				else
				{
					auto pp = l+1+rr*2;
					index[e] = pp;
					pos[pp] = e;
					++rr;
				}
			}
			l += set[j].length;
		}
		if (ok)
		{
			long t;
			auto arr = new long[](N);
			foreach (j; 0..N) arr[j] = j;
			foreach (j; 0..N)
			{
				foreach (k; j..N)
				{
					if (arr[k] != pos[j]) continue;
					t += k-j;
					foreach_reverse (m; j..k)
					{
						swap(arr[m], arr[m+1]);
					}
				}
			}
			debug writeln(set);
			debug writeln(eo);
			debug writeln(index);
			debug writeln(t);
			ans.chmin(t);
		}
	}

	if (ans == long.max)
		writeln(-1);
	else
		writeln(ans);
	stdout.flush;
	debug readln;
}


Measured memory_bytes: 1276