Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    all_cards.append((aa, bb, i))

def check(cards):
    pre = -1
    for c in cards:
        print(c)
        if c[0] >= pre:
            pre = c[0]
        else:
            return False
    else:
        return True

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i] > l[j]:
                ret += 1
    return ret

def dp(all_cards, l, p):
    i = len(l) + 1
    if len(l) == n:
        return cnt(l)
    ret = float('inf')
    for card in all_cards:
        if card[2] not in l and card[0] < p and card[1] < p:
            return ret
    for card in all_cards:
        if card[2] in l:
            continue
        if card[(i-(card[2]+1))%2] >= p:
            ret = min(ret, dp(all_cards - set(card), l+[card[2]], card[(i-(card[2]+1))%2]))
    return ret

ans = dp(set(all_cards), [], -float('inf'))
if ans == float('inf'):
    ans = -1
print(ans)

Fixed submission:
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    if i % 2:
        aa, bb = bb, aa
    all_cards.append([aa, bb, i])

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i][1] > l[j][1]:
                ret += 1
    return ret

ans = float('inf')
for cards in combinations(all_cards, (n + 1)// 2):
    acards = []
    for card in cards:
        acards.append([card[0],card[2]])
    bcards = []
    for card in all_cards:
        if [card[0],card[2]] not in acards:
            bcards.append([card[1],card[2]])

    acards.sort()
    bcards.sort()
    abcards = []
    # print(acards, bcards)
    for i in range(len(bcards)):
        abcards.extend([acards[i], bcards[i]])
    else:
        if n % 2:
            abcards.append(acards[-1])
    # print(abcards)
    if abcards != sorted(abcards, key=lambda x: x[0]):
        continue
    ans = min(ans, cnt(abcards))

if ans == float('inf'):
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    all_cards.append((aa, bb, i))

def check(cards):
    pre = -1
    for c in cards:
        print(c)
        if c[0] >= pre:
            pre = c[0]
        else:
            return False
    else:
        return True

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i] > l[j]:
                ret += 1
    return ret

dic = {}
def dp(all_cards, l, p):
    i = len(l) + 1
    if len(l) == n:
        return cnt(l)
    ret = float('inf')
    for card in all_cards:
        if card[2] not in l and card[0] < p and card[1] < p:
            return ret
    for card in all_cards:
        if card[2] in l:
            continue
        if card[(i-(card[2]+1))%2] >= p:
            ret = min(ret, dp(all_cards - set(card), l+[card[2]], card[(i-(card[2]+1))%2]))
    dic[tuple(l)] = ret
    return ret

ans = dp(set(all_cards), [], -float('inf'))
if ans == float('inf'):
    ans = -1
print(ans)

Fixed submission:
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    if i % 2:
        aa, bb = bb, aa
    all_cards.append([aa, bb, i])

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i][1] > l[j][1]:
                ret += 1
    return ret

ans = float('inf')
for cards in combinations(all_cards, (n + 1)// 2):
    acards = []
    for card in cards:
        acards.append([card[0],card[2]])
    bcards = []
    for card in all_cards:
        if [card[0],card[2]] not in acards:
            bcards.append([card[1],card[2]])

    acards.sort()
    bcards.sort()
    abcards = []
    # print(acards, bcards)
    for i in range(len(bcards)):
        abcards.extend([acards[i], bcards[i]])
    else:
        if n % 2:
            abcards.append(acards[-1])
    # print(abcards)
    if abcards != sorted(abcards, key=lambda x: x[0]):
        continue
    ans = min(ans, cnt(abcards))

if ans == float('inf'):
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    all_cards.append((aa, bb, i))

def check(cards):
    pre = -1
    for c in cards:
        print(c)
        if c[0] >= pre:
            pre = c[0]
        else:
            return False
    else:
        return True

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i] > l[j]:
                ret += 1
    return ret

dic = {}
def dp(all_cards, l, p):
    i = len(l) + 1
    if len(l) == n:
        return cnt(l)
    ret = float('inf')
    for card in all_cards:
        if card[2] in l:
            continue
        if card[(i-(card[2]+1))%2] >= p:
            ret = min(ret, dp(all_cards - set(card), l+[card[2]], card[(i-(card[2]+1))%2]))
    dic[tuple(l)] = ret
    return ret

ans = dp(set(all_cards), [], -float('inf'))
if ans == float('inf'):
    ans = -1
print(ans)

Fixed submission:
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    if i % 2:
        aa, bb = bb, aa
    all_cards.append([aa, bb, i])

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i][1] > l[j][1]:
                ret += 1
    return ret

ans = float('inf')
for cards in combinations(all_cards, (n + 1)// 2):
    acards = []
    for card in cards:
        acards.append([card[0],card[2]])
    bcards = []
    for card in all_cards:
        if [card[0],card[2]] not in acards:
            bcards.append([card[1],card[2]])

    acards.sort()
    bcards.sort()
    abcards = []
    # print(acards, bcards)
    for i in range(len(bcards)):
        abcards.extend([acards[i], bcards[i]])
    else:
        if n % 2:
            abcards.append(acards[-1])
    # print(abcards)
    if abcards != sorted(abcards, key=lambda x: x[0]):
        continue
    ans = min(ans, cnt(abcards))

if ans == float('inf'):
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    all_cards.append((aa, bb, i))

def check(cards):
    pre = -1
    for c in cards:
        print(c)
        if c[0] >= pre:
            pre = c[0]
        else:
            return False
    else:
        return True

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i] > l[j]:
                ret += 1
    return ret

dic = {}
def dp(all_cards, l, p):
    i = len(l) + 1
    if len(l) == n:
        return cnt(l)
    ret = float('inf')
    for card in all_cards:
        if card[2] in l:
            continue
        if card[(i-(card[2]+1))%2] >= p:
            ret = min(ret, dp(all_cards, l+[card[2]], card[(i-(card[2]+1))%2]))
    dic[tuple(l)] = ret
    return ret

ans = dp(all_cards, [], -float('inf'))
if ans == float('inf'):
    ans = -1
print(ans)

Fixed submission:
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    if i % 2:
        aa, bb = bb, aa
    all_cards.append([aa, bb, i])

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i][1] > l[j][1]:
                ret += 1
    return ret

ans = float('inf')
for cards in combinations(all_cards, (n + 1)// 2):
    acards = []
    for card in cards:
        acards.append([card[0],card[2]])
    bcards = []
    for card in all_cards:
        if [card[0],card[2]] not in acards:
            bcards.append([card[1],card[2]])

    acards.sort()
    bcards.sort()
    abcards = []
    # print(acards, bcards)
    for i in range(len(bcards)):
        abcards.extend([acards[i], bcards[i]])
    else:
        if n % 2:
            abcards.append(acards[-1])
    # print(abcards)
    if abcards != sorted(abcards, key=lambda x: x[0]):
        continue
    ans = min(ans, cnt(abcards))

if ans == float('inf'):
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    all_cards.append((aa, bb, i))

def check(cards):
    pre = -1
    for c in cards:
        print(c)
        if c[0] >= pre:
            pre = c[0]
        else:
            return False
    else:
        return True

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i] > l[j]:
                ret += 1
    return ret

def dp(all_cards, l, p):
    i = len(l) + 1
    if len(l) == n:
        return cnt(l)
    ret = float('inf')
    for card in all_cards:
        if card[2] in l:
            continue
        if card[(i-(card[2]+1))%2] >= p:
            ret = min(ret, dp(all_cards, l+[card[2]], card[(i-(card[2]+1))%2]))
    return ret

ans = dp(all_cards, [], -float('inf'))
if ans == float('inf'):
    ans = -1
print(ans)

Fixed submission:
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    if i % 2:
        aa, bb = bb, aa
    all_cards.append([aa, bb, i])

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i][1] > l[j][1]:
                ret += 1
    return ret

ans = float('inf')
for cards in combinations(all_cards, (n + 1)// 2):
    acards = []
    for card in cards:
        acards.append([card[0],card[2]])
    bcards = []
    for card in all_cards:
        if [card[0],card[2]] not in acards:
            bcards.append([card[1],card[2]])

    acards.sort()
    bcards.sort()
    abcards = []
    # print(acards, bcards)
    for i in range(len(bcards)):
        abcards.extend([acards[i], bcards[i]])
    else:
        if n % 2:
            abcards.append(acards[-1])
    # print(abcards)
    if abcards != sorted(abcards, key=lambda x: x[0]):
        continue
    ans = min(ans, cnt(abcards))

if ans == float('inf'):
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    all_cards.append((aa, bb, i))

def check(cards):
    pre = -1
    for c in cards:
        print(c)
        if c[0] >= pre:
            pre = c[0]
        else:
            return False
    else:
        return True

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i] > l[j]:
                ret += 1
    return ret

def dp(all_cards, l, p):
    i = len(l) + 1
    if len(l) == n:
        return cnt(l)
    ret = float('inf')
    for card in all_cards:
        if card[2] in l:
            continue
        if card[(i-(card[2]+1))%2] >= p:
            ret = min(ret, dp(all_cards, l+[card[2]], card[(i-(card[2]+1))%2]))
    return ret

ans = dp(all_cards, [], -float('inf'))
if ans == float('inf'):
    ans = -1
print(ans)

Fixed submission:
import sys
from itertools import combinations
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]

all_cards = []
for i, (aa, bb) in enumerate(zip(a, b)):
    if i % 2:
        aa, bb = bb, aa
    all_cards.append([aa, bb, i])

def cnt(l):
    ret = 0
    for i in range(len(l)-1):
        for j in range(i+1, len(l)):
            if l[i][1] > l[j][1]:
                ret += 1
    return ret

ans = float('inf')
for cards in combinations(all_cards, (n + 1)// 2):
    acards = []
    for card in cards:
        acards.append([card[0],card[2]])
    bcards = []
    for card in all_cards:
        if [card[0],card[2]] not in acards:
            bcards.append([card[1],card[2]])

    acards.sort()
    bcards.sort()
    abcards = []
    # print(acards, bcards)
    for i in range(len(bcards)):
        abcards.extend([acards[i], bcards[i]])
    else:
        if n % 2:
            abcards.append(acards[-1])
    # print(abcards)
    if abcards != sorted(abcards, key=lambda x: x[0]):
        continue
    ans = min(ans, cnt(abcards))

if ans == float('inf'):
    ans = -1
print(ans)