Buggy python submission (verdict: Time Limit Exceeded):
def main():
  N = int(input())
  A = list(map(int,input().split()))
  B = list(map(int,input().split()))
  # M:カードの数字の最大値+1
  M = max(max(A),max(B))+1
  INF=float('inf')
  # dp[mask][a]:maskのカードが操作済で、そのうち右端のカードの数字がaであるときの操作回数の最小値
  dp = [[INF for _ in range(M)] for _ in range(1<<N)]
  dp[0][0] = 0
  
  for i in range(1<<N):
    # idx:操作済カードの枚数
    idx = 0
    for k in range(N):
      #k番目のカードが操作済みかどうか判定する。iでk番目にフラグがたってたら、操作済みとする。
      if (i>>k)&1 == 1:
        idx += 1
    for j in range(M):
      if dp[i][j] >= INF:
        continue
      for k in range(N):
        # k:右端の操作済カードの次に来るカード
        # kがすでに操作済の場合はcontinueする
        
        # kが何回操作されるかによってkの数字が表の数字になるか裏の数字になるかが決まる
        if abs(k-idx)%2 == 0:
          c = A[k]
        else:
          c = B[k]
  
        # kの数字広義単調増加にならない場合はcontinue
        if j > c:
          continue
        
        # t:kを右端の操作済カードの次に並べるための操作回数
        # それは、初期配置でkより左にあってまだ操作済でないカードの枚数と一致する
        t = 0
        for l in range(k):
          if (i>>l)&1 == 0:
            t += 1
        #print(i,k,idx,c,dp[i|(1<<k)][c],t)
        dp[i|(1<<k)][c] = min(dp[i|(1<<k)][c],dp[i][j]+t)
  
  ans = INF
  for i in range(M):
    ans = min(dp[(1<<N)-1][i],ans)
  print(ans if ans < INF else -1)

if __name__ == '__main__':
  main()

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
judge = False
ans = 10**9
for i in range(2**N):
  even = []
  odd = []
  x = 0
  tmp = True
  for j in range(N):
    if ((i>>j) & 1):
      if j%2==1:
        even.append([B[j],j])
      else:
        odd.append([B[j],j])
      
    else:
      if j%2==1:
        odd.append([A[j],j])
      else:
        even.append([A[j],j])
        
  if N%2==0:
    if len(even)!=len(odd):
      continue
      
  else:
    if (len(even)-1)!=len(odd):
      continue
      

  even = sorted(even,key = lambda x:x[0])
  odd = sorted(odd,key = lambda x:x[0])
  ind = [0]*N
  for k in range(N//2):
    o = odd[k]
    e = even[k]
    if o[0]<e[0]:
      tmp = False
      break
    
    if (k!=N//2-1)or(N%2==1):
      if o[0]>even[k+1][0]:
        tmp = False
        break
    
    ind[2*k]=e[1]
    ind[2*k+1]=o[1]

  if tmp:
    if N%2==1:
      ind[N-1]=even[-1][1]
      

    for k in range(N):
      y = ind[k]
      x+=abs(y-k)
      ind = [x+1 if (x<y)and(x>=k) else x for x in ind]
    ans = min(ans,x)
    judge = True
    
if judge:
  print(ans)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
judge = False
ans = 10**9
for i in range(2**N):
  even = []
  odd = []
  x = 0
  tmp = True
  for j in range(N):
    if ((i>>j) & 1):
      if j%2==1:
        even.append([B[j],j])
      else:
        odd.append([B[j],j])
      
    else:
      if j%2==1:
        odd.append([A[j],j])
      else:
        even.append([A[j],j])
        
  if N%2==0:
    if len(even)!=len(odd):
      continue
      
  else:
    if (len(even)-1)!=len(odd):
      continue
      

  even = sorted(even,key = lambda x:x[0])
  odd = sorted(odd,key = lambda x:x[1])
  ind = [0]*N
  for k in range(N//2):
    o = odd[k]
    e = even[k]
    if (o[0]<e[0])or(o[0]>even[k+1][0]):
      tmp = False
      break
  
    else:
      ind[2*k]=e[1]
      ind[2*k+1]=o[1]
      
  if tmp:
    if N%2==1:
      ind[N-1]=even[-1][1]
    for k in range(N):
      y = ind[k]
      x+=abs(y-k)
      ind = [x+1 if (x<y)and(x>=k) else x for x in ind]
    ans = min(ans,x)
    judge = True
    
if judge:
  print(ans)
else:
  print(-1)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
judge = False
ans = 10**9
for i in range(2**N):
  even = []
  odd = []
  x = 0
  tmp = True
  for j in range(N):
    if ((i>>j) & 1):
      if j%2==1:
        even.append([B[j],j])
      else:
        odd.append([B[j],j])
      
    else:
      if j%2==1:
        odd.append([A[j],j])
      else:
        even.append([A[j],j])
        
  if N%2==0:
    if len(even)!=len(odd):
      continue
      
  else:
    if (len(even)-1)!=len(odd):
      continue
      

  even = sorted(even,key = lambda x:x[0])
  odd = sorted(odd,key = lambda x:x[0])
  ind = [0]*N
  for k in range(N//2):
    o = odd[k]
    e = even[k]
    if o[0]<e[0]:
      tmp = False
      break
    
    if (k!=N//2-1)or(N%2==1):
      if o[0]>even[k+1][0]:
        tmp = False
        break
    
    ind[2*k]=e[1]
    ind[2*k+1]=o[1]

  if tmp:
    if N%2==1:
      ind[N-1]=even[-1][1]
      

    for k in range(N):
      y = ind[k]
      x+=abs(y-k)
      ind = [x+1 if (x<y)and(x>=k) else x for x in ind]
    ans = min(ans,x)
    judge = True
    
if judge:
  print(ans)
else:
  print(-1)