from itertools import combinations

n = int(eval(input()))

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

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



even = []   #偶数番目で表になる

odd = []    #奇数番目で表になる

for i,(a,b) in enumerate(zip(As,Bs)):

    if i%2 == 0:

        even.append(a)

        odd.append(b)

    else:

        even.append(b)

        odd.append(a)







def check(n, even, odd):

    prev = -10**10

    ps = []

    for i in range(n):

        if i%2 == 0:

            if prev > even[i//2][0]:

                return False,-1

            else:

                ps.append(even[i//2][1])

            prev = even[i//2][0]

        else:

            if prev > odd[i//2][0]:

                return  False, -1

            else:

                ps.append(odd[i//2][1])

            prev = odd[i//2][0]

    return True, ps

    





def inversion(a):

    n = len(a)

    cnt = 0

    for i in range(n):

        for j in range(i+1,n):

            if a[i] > a[j]:

                cnt +=1

    return cnt

 

#偶数としてpickupするやつ

INF = 10**10

ans = 10**10

for l in combinations(list(range(n)), (n-1)//2+1):

    cnt = 0

    even_selected = [(even[i],i) for i in range(n) if i in l]

    odd_selected = [(odd[i],i) for i in range(n) if i not in l]

    # こいつらを昇順に並べられるか

    even_selected.sort()

    odd_selected.sort()

    is_possible, a = check(n, even_selected, odd_selected)

    # 並び順が正しければ

    if is_possible:

        cnt = inversion(a)

        ans = min(cnt, ans)

        

if ans == INF:

    print((-1))

else:

    print(ans)        
n = int(eval(input()))

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

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



INF = 1<<20



dp = [[INF] * 55 for i in range(1<<n)]

dp[0][0] = 0



for b in range(1<<n):

    # 使ったカードの枚数

    bitcnt = bin(b).count("1")

    cur = bitcnt 

    rest = [] 

    for i in range(n):

        # i番目のbitが立っているかの判定

        if not (b & (1 << i)):

            rest.append((i,cur))

            cur += 1

    

    for s in range(51):

        if dp[b][s] >= INF:

            continue

        # 残ったカードで更新

        for i,cur in rest:

            if abs(bitcnt - i) % 2 == 0:

                num = As[i]

            else:

                num = Bs[i]

            if num >= s:

                d = abs(bitcnt - cur) 

                dp[b|(1<<i)][num] = min(dp[b|(1<<i)][num], dp[b][s] + d)

            



ans = min(dp[(1<<n)-1])

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

        

    

from itertools import combinations

n = int(eval(input()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
even = []  # 偶数番目で表になる
odd = []  # 奇数番目で表になる
for i, (a, b) in enumerate(zip(As, Bs)):
    if i % 2 == 0:
        even.append(a)
        odd.append(b)
    else:
        even.append(b)
        odd.append(a)


def check(n, even, odd):
    prev = -(10**10)
    ps = []
    for i in range(n):
        if i % 2 == 0:
            if prev > even[i // 2][0]:
                return False, -1
            else:
                ps.append(even[i // 2][1])
            prev = even[i // 2][0]
        else:
            if prev > odd[i // 2][0]:
                return False, -1
            else:
                ps.append(odd[i // 2][1])
            prev = odd[i // 2][0]
    return True, ps


def inversion(a):
    n = len(a)
    cnt = 0
    for i in range(n):
        for j in range(i + 1, n):
            if a[i] > a[j]:
                cnt += 1
    return cnt


# 偶数としてpickupするやつ
INF = 10**10
ans = 10**10
for l in combinations(list(range(n)), (n - 1) // 2 + 1):
    cnt = 0
    even_selected = [(even[i], i) for i in range(n) if i in l]
    odd_selected = [(odd[i], i) for i in range(n) if i not in l]
    # こいつらを昇順に並べられるか
    even_selected.sort()
    odd_selected.sort()
    is_possible, a = check(n, even_selected, odd_selected)
    # 並び順が正しければ
    if is_possible:
        cnt = inversion(a)
        ans = min(cnt, ans)
if ans == INF:
    print((-1))
else:
    print(ans)

n = int(eval(input()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
INF = 1 << 20
dp = [[INF] * 55 for i in range(1 << n)]
dp[0][0] = 0
for b in range(1 << n):
    # 使ったカードの枚数
    bitcnt = bin(b).count("1")
    cur = bitcnt
    rest = []
    for i in range(n):
        # i番目のbitが立っているかの判定
        if not (b & (1 << i)):
            rest.append((i, cur))
            cur += 1
    for s in range(51):
        if dp[b][s] >= INF:
            continue
        # 残ったカードで更新
        for i, cur in rest:
            if abs(bitcnt - i) % 2 == 0:
                num = As[i]
            else:
                num = Bs[i]
            if num >= s:
                d = abs(bitcnt - cur)
                dp[b | (1 << i)][num] = min(dp[b | (1 << i)][num], dp[b][s] + d)
ans = min(dp[(1 << n) - 1])
print((ans if ans != INF else -1))

-from itertools import combinations - -even = []  # 偶数番目で表になる -odd = []  # 奇数番目で表になる -for i, (a, b) in enumerate(zip(As, Bs)): -    if i % 2 == 0: -        even.append(a) -        odd.append(b) -    else: -        even.append(b) -        odd.append(a) - - -def check(n, even, odd): -    prev = -(10**10) -    ps = [] +INF = 1 << 20 +dp = [[INF] * 55 for i in range(1 << n)] +dp[0][0] = 0 +for b in range(1 << n): +    # 使ったカードの枚数 +    bitcnt = bin(b).count("1") +    cur = bitcnt +    rest = [] -        if i % 2 == 0: -            if prev > even[i // 2][0]: -                return False, -1 +        # i番目のbitが立っているかの判定 +        if not (b & (1 << i)): +            rest.append((i, cur)) +            cur += 1 +    for s in range(51): +        if dp[b][s] >= INF: +            continue +        # 残ったカードで更新 +        for i, cur in rest: +            if abs(bitcnt - i) % 2 == 0: +                num = As[i] -                ps.append(even[i // 2][1]) -            prev = even[i // 2][0] -        else: -            if prev > odd[i // 2][0]: -                return False, -1 -            else: -                ps.append(odd[i // 2][1]) -            prev = odd[i // 2][0] -    return True, ps - - -def inversion(a): -    n = len(a) -    cnt = 0 -    for i in range(n): -        for j in range(i + 1, n): -            if a[i] > a[j]: -                cnt += 1 -    return cnt - - -# 偶数としてpickupするやつ -INF = 10**10 -ans = 10**10 -for l in combinations(list(range(n)), (n - 1) // 2 + 1): -    cnt = 0 -    even_selected = [(even[i], i) for i in range(n) if i in l] -    odd_selected = [(odd[i], i) for i in range(n) if i not in l] -    # こいつらを昇順に並べられるか -    even_selected.sort() -    odd_selected.sort() -    is_possible, a = check(n, even_selected, odd_selected) -    # 並び順が正しければ -    if is_possible: -        cnt = inversion(a) -        ans = min(cnt, ans) -if ans == INF: -    print((-1)) -else: -    print(ans) +                num = Bs[i] +            if num >= s: +                d = abs(bitcnt - cur) +                dp[b | (1 << i)][num] = min(dp[b | (1 << i)][num], dp[b][s] + d) +ans = min(dp[(1 << n) - 1]) +print((ans if ans != INF else -1))