from itertools import permutations, combinations, chain

from collections import defaultdict

N = int(eval(input()))

A = list(map(int,input().split()))

B = list(map(int,input().split()))



def dpinit(ps, val=0):

    import copy

    res = [val for i in [0]*ps[-1]]

    for i in ps[:-1][::-1]:

        res = [copy.deepcopy(res) for k in [0]*i]

    return res



inf = 10**10

dp = [defaultdict(lambda : inf) for i in range(2**N+1)]

dp[0][0] = 0



for b in range(2**N):

    c = bin(b).count('1')

    for i in range(1,N+1):

        p = N-i

        f = 1<<p

        if b & f:

            continue

        nb = b | f

        k = bin(b % f).count('1')

        v = A[i-1] if (i-c) % 2 == 1 else B[i-1]

        for val, inv in list(dp[b].items()):

            if val <= v:

                dp[nb][v] = min(dp[nb][v], inv+abs(c+1-(i+k)))



if not dp[2**N-1]:

    print((-1))

    exit()

ans = min(dp[2**N-1].values())

print((ans if ans != inf else -1))





from itertools import chain

N = int(eval(input()))

A = list(map(int,input().split()))

B = list(map(int,input().split()))



def count_bit(x):

    res = 0

    while x:

        res += x % 2

        x >>= 1

    return res



def int2bin(x,N): 

    res = [0]*N

    for i in range(N):

        res[N-1-i] = x % 2

        x >>= 1

    return res



inf = 10**10

M = N//2 # the n of odd index

ans = inf

for b in range(2**N):

    valid = True 

    if count_bit(b) == M:

        odd_index = int2bin(b,N) # 1 : odd

        evens = []

        odds = []

        for i in range(N):

            if odd_index[i]:

                if i % 2:

                    odds.append((A[i],i))

                else:

                    odds.append((B[i],i))

            else:

                if i % 2:

                    evens.append((B[i],i))

                else:

                    evens.append((A[i],i))

        SE = sorted(evens)

        SO = sorted(odds)

        L = [None]*N

        L[::2] = SE

        L[1::2] = SO

        for (a,_),(b,_) in zip(L,L[1:]):

            if a > b:

                valid = False

                break

        if valid:

            L = list(zip(L,list(range(N))))

            L = sorted(L,key=lambda x : x[0][1])

            L = [(a,c) for (a,b),c in L]

            temp = 0

            for i in range(N):

                for j in range(i):

                    if L[j] > L[i]:

                        temp += 1

            ans = min(temp,ans)



if ans == inf:

    ans = -1

print(ans)







from itertools import permutations, combinations, chain
from collections import defaultdict

N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))


def dpinit(ps, val=0):
    import copy

    res = [val for i in [0] * ps[-1]]
    for i in ps[:-1][::-1]:
        res = [copy.deepcopy(res) for k in [0] * i]
    return res


inf = 10**10
dp = [defaultdict(lambda: inf) for i in range(2**N + 1)]
dp[0][0] = 0
for b in range(2**N):
    c = bin(b).count("1")
    for i in range(1, N + 1):
        p = N - i
        f = 1 << p
        if b & f:
            continue
        nb = b | f
        k = bin(b % f).count("1")
        v = A[i - 1] if (i - c) % 2 == 1 else B[i - 1]
        for val, inv in list(dp[b].items()):
            if val <= v:
                dp[nb][v] = min(dp[nb][v], inv + abs(c + 1 - (i + k)))
if not dp[2**N - 1]:
    print((-1))
    exit()
ans = min(dp[2**N - 1].values())
print((ans if ans != inf else -1))

from itertools import chain

N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))


def count_bit(x):
    res = 0
    while x:
        res += x % 2
        x >>= 1
    return res


def int2bin(x, N):
    res = [0] * N
    for i in range(N):
        res[N - 1 - i] = x % 2
        x >>= 1
    return res


inf = 10**10
M = N // 2  # the n of odd index
ans = inf
for b in range(2**N):
    valid = True
    if count_bit(b) == M:
        odd_index = int2bin(b, N)  # 1 : odd
        evens = []
        odds = []
        for i in range(N):
            if odd_index[i]:
                if i % 2:
                    odds.append((A[i], i))
                else:
                    odds.append((B[i], i))
            else:
                if i % 2:
                    evens.append((B[i], i))
                else:
                    evens.append((A[i], i))
        SE = sorted(evens)
        SO = sorted(odds)
        L = [None] * N
        L[::2] = SE
        L[1::2] = SO
        for (a, _), (b, _) in zip(L, L[1:]):
            if a > b:
                valid = False
                break
        if valid:
            L = list(zip(L, list(range(N))))
            L = sorted(L, key=lambda x: x[0][1])
            L = [(a, c) for (a, b), c in L]
            temp = 0
            for i in range(N):
                for j in range(i):
                    if L[j] > L[i]:
                        temp += 1
            ans = min(temp, ans)
if ans == inf:
    ans = -1
print(ans)

-from itertools import permutations, combinations, chain -from collections import defaultdict +from itertools import chain -def dpinit(ps, val=0): -    import copy +def count_bit(x): +    res = 0 +    while x: +        res += x % 2 +        x >>= 1 +    return res -    res = [val for i in [0] * ps[-1]] -    for i in ps[:-1][::-1]: -        res = [copy.deepcopy(res) for k in [0] * i] + +def int2bin(x, N): +    res = [0] * N +    for i in range(N): +        res[N - 1 - i] = x % 2 +        x >>= 1 -dp = [defaultdict(lambda: inf) for i in range(2**N + 1)] -dp[0][0] = 0 +M = N // 2  # the n of odd index +ans = inf -    c = bin(b).count("1") -    for i in range(1, N + 1): -        p = N - i -        f = 1 << p -        if b & f: -            continue -        nb = b | f -        k = bin(b % f).count("1") -        v = A[i - 1] if (i - c) % 2 == 1 else B[i - 1] -        for val, inv in list(dp[b].items()): -            if val <= v: -                dp[nb][v] = min(dp[nb][v], inv + abs(c + 1 - (i + k))) -if not dp[2**N - 1]: -    print((-1)) -    exit() -ans = min(dp[2**N - 1].values()) -print((ans if ans != inf else -1)) +    valid = True +    if count_bit(b) == M: +        odd_index = int2bin(b, N)  # 1 : odd +        evens = [] +        odds = [] +        for i in range(N): +            if odd_index[i]: +                if i % 2: +                    odds.append((A[i], i)) +                else: +                    odds.append((B[i], i)) +            else: +                if i % 2: +                    evens.append((B[i], i)) +                else: +                    evens.append((A[i], i)) +        SE = sorted(evens) +        SO = sorted(odds) +        L = [None] * N +        L[::2] = SE +        L[1::2] = SO +        for (a, _), (b, _) in zip(L, L[1:]): +            if a > b: +                valid = False +                break +        if valid: +            L = list(zip(L, list(range(N)))) +            L = sorted(L, key=lambda x: x[0][1]) +            L = [(a, c) for (a, b), c in L] +            temp = 0 +            for i in range(N): +                for j in range(i): +                    if L[j] > L[i]: +                        temp += 1 +            ans = min(temp, ans) +if ans == inf: +    ans = -1 +print(ans)