Harryxun/stf_run huggingface dataset:
from collections import Counter
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    CNT = Counter(A + B)
    num = CNT.most_common()[0][1]
    if num > N:
        print("No")
        return

    j = 0
    for i in range(N):
        if A[i] == B[i]:
            for _ in range(N):
                j += 1
                if A[i] != B[j] and A[j] != B[i]:
                    B[i], B[j] = B[j], B[i]
                    break
                if j == N - 1:
                    j = -1

    print('Yes')
    print(*B)


if __name__ == "__main__":
    resolve()

### FIXED CODE ###


from collections import Counter
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    CNT = Counter(A+B)
    num = CNT.most_common()[0][1]
    if num > N:
        print("No")
        return 

    j, pre = 0, -1
    for i in range(N):
        if A[i] != pre:
            j = 0
        if A[i] == B[i]:
            while j < N:
                if A[i] != B[j] and A[j] != B[i]:
                    B[i], B[j] = B[j], B[i]
                    break
                j += 1
        pre = A[i]

    print('Yes')
    print(*B)


if __name__ == "__main__":
    resolve()
import math

a, b = map(int, input().split())

#最大公約数: greatest common divisor(gcd)
g = math.gcd(a,b)
print(g)
def prime_decomposition(n):
  i=2
  table = []
  while i*i <= n:
    while n%i==0:
      n//=i
      table.append(i)
    i+=1
  if n>1:
    table.append(n)
  return table
print(len(set(prime_decomposition(g)))+1)

### FIXED CODE ###

# import math
from fractions import gcd

a, b = map(int, input().split())

#最大公約数: greatest common divisor(gcd)
# g = math.gcd(a,b)
g=gcd(a,b)

def prime_decomposition(n):
  i=2
  table = []
  while i*i <= n:
    while n%i==0:
      n//=i
      table.append(i)
    i+=1
  if n>1:
    table.append(n)
  return table
print(len(set(prime_decomposition(g)))+1)
a, b = input()
print a*b, 2*(a+b)

### FIXED CODE ###

a, b = map(int, raw_input().split())
print a*b, 2*a+2*b
n = int(input())
c = list(map(int, input().split()))
ln = []
if c.count(1) == 0:
    print(-1)
else:
    c2 = c
    for i in range (1, max(c)+1):
        if i in c2 :
            s = c2.index(i)
            ln.append(i)
            del c2[:s + 1]
            if len(c2) == 1 and  i + 1 in c2:
                    ln.append(i + 1)
        else:
            break
print(n - max(ln))   

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
now = 1
brok = 0
for i in range (n):
    if(A[i] != now):
        brok += 1
    else:
        now += 1
if(now == 1):
    print(-1)
else:
    print(brok)
a = int(input())
b,c = map(int, input().split())
k = int(input())
n = 0

for _ in range(k):
  if a > 0:
    n += 1
  elif b > 0:
    n += 0
  elif c > 0:
    n -= 1
    
print(n)

### FIXED CODE ###

a, b, c, k = map(int, input().split())

if k <= a:
  print(k)
elif a < k <= a+b:
  print(a)
elif a+b < k <= a+b+c:
  print(2*a + b - k)
A,B=map(int,input().split())
count=0
if i>=A and i<=B:
    for j in i:
        array_i=list(map(int,str(i)))
        if array_i==reverse(array_i):
            count+=1
print(count)

### FIXED CODE ###

A,B=map(int,input().split())
count=0
for i in range(A,B+1):
        if str(i)==str(i)[::-1]:
            count+=1
print(count)
A=int(input())
B=int(input())
C=int(input())

print(A[0],B[1],C[2])

### FIXED CODE ###


U=input()
M=input()
B=input()

print(U[0]+M[1]+B[2])
import sys
sys.setrecursionlimit(10**6)
def d(n):
 if n==0:return 0
 return d(n%bin(n).count("1"))+1
n=int(input())
x=input()
c=x.count("1")
a=int(x,2)
j,k=a%(c+1),a%(c-1)
for i in range(n):
 if x[i]=="1":print(d((k-pow(2,n-i-1,c-1))%(c-1))+1)
 else:print(d((j+pow(2,n-i-1,c+1))%(c+1))+1)

### FIXED CODE ###

def dfs(n):
    if n == 0:return 0
    return dfs(n % format(n, "b").count("1")) + 1
n = int(input())
x = input()
c = x.count("1")
a = int(x, 2)
if c == 1:
    for i in range(n):
        if x[i] == "1":
            print(0)
        elif i == n - 1:
            print(2)
        else:
            print(1)
else:
    j, k = a % (c + 1), a % (c - 1)
    for i in range(n):
        if x[i] == "1":print(dfs((k - pow(2, n - i - 1, c - 1)) % (c - 1)) + 1)
        else:print(dfs((j + pow(2, n - i - 1, c + 1)) % (c + 1)) + 1)
n = input()
print(n * n * n)

### FIXED CODE ###

n = int(input())
print(n ** 3)
s, t = map(str, input().split())
a, b = map(int, input().split())
u = input()
if u == s:
  a -= 1
 else:
  b -= 1
 print(a, b)

### FIXED CODE ###

s, t = map(str, input().split())
a, b = map(int, input().split())
u = input()
if u == s:
  a -= 1
else:
  b -= 1
print(a, b)
iN = int(input())
aS = map(int,input().split())

iCounter = 0
for i in range(1,iN-1):
  if aS[i] == aS[i-1]:
    if aS[i] == aS[i+1]:
      aS[i] += 1
      iCounter += 1
    else:
      iU = max(aS[i],aS[i+1])
      if iU < 10000:
        iU += 1
      else:
        while iU not in aS[i:i+2]:
          iU -= 1
      aS[i] = iU
      iCounter += 1
print(iCounter)

### FIXED CODE ###

iN = int(input())
aS = [int(x) for x in input().rstrip().split()]

#はぁ こんなのを一発で通れるようなものも書けないのか.. つらい

#つけたしつけたしで条件追加しててやっててつらい



iCounter = 0
if iN == 2:
    if aS[0] == aS[1]:
        iCounter += 1
else:
    for i in range(1,iN-1):
        if aS[i] == aS[i-1]:
            if aS[i] == aS[i+1]:
                if aS[i] < 10000:
                    aS[i] += 1
                else:
                    aS[i] -= 1
                iCounter += 1
            else:
                iU = max(aS[i],aS[i+1])
                if iU < 10000:
                    iU += 1
                else:
                    iU -= 1
                    if iU == min(aS[1],aS[i+1]):
                        iU -= 1
                aS[i] = iU
                iCounter += 1
    if aS[iN-2] == aS[iN-1] :
        iCounter += 1
print(iCounter)
import decimal
import math
a, b, c = map(int, input().split())

if  decimal.Decimal(math.sqrt(c)) - decimal.Decimal(math.sqrt(a)) + decimal.Decimal(math.sqrt(b)) > 0:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

import decimal
import math
a, b, c = map(int, input().split())

d = c - a - b

if d > 0 and d * d > 4 * a * b:
    print("Yes")
else:
    print("No")
n = int(input())
b = list(map(int,input().split()))
ans = 0

for i in range(n-2):
    ans += min(b[i],b[i+1])  

if n == 2:
    print(min(b)*2)
else:
    print(ans+b[-1]+min(b))

### FIXED CODE ###

n = int(input())
print(n*n*n)
A, B, K = map(int,input().split())
if(A>=k):
  print(A-k,B)
elif(B-K+A>=0):
  print(0,B-K+A)
else:
  print(0,0)

### FIXED CODE ###

S, T=input().split()
print(T+S)
import networkx as nx

N = int(input())
ABs = [tuple(map(int, input().split())) for _ in range(N)]
CDs = [tuple(map(int, input().split())) for _ in range(N)]

# 0 <= i <   N  -> i: ABs[i]
# N <= i < 2*N  -> i: CDs[i-N]
# i == 2*N+1    -> start
# i == 2*N+2    -> goal

DG = nx.DiGraph()
DG.add_nodes_from(range(2 * N + 3))
for i in range(N):
    for j in range(N):
        if ABs[i][0] < CDs[j][0] and ABs[i][1] < CDs[j][1]:
            DG.add_edge(i, j + N, capacity = 1)

DG.add_edges_from([(2 * N + 1, i, {"capacity":1}) for i in range(N)])
DG.add_edges_from([(i, 2 * N + 2, {"capacity":1}) for i in range(N, 2*N)])

print(nx.maximum_flow_value(DG, 2 * N + 1, 2 * N + 2))


### FIXED CODE ###

N = int(input())
AB = [tuple(map(int, input().split())) for _ in range(N)]
CD = [tuple(map(int, input().split())) for _ in range(N)]

AB.sort(key = max, reverse = True)
CD.sort(key = min)

counter = 0
for a, b in AB:
    for c, d in CD:
        if a < c and b < d:
            CD.remove( (c, d) )
            counter += 1
            break

print(counter)
N = int(input())
A = input().split()

for i in range(1,N+1):
    print(A.count(str(i)))


### FIXED CODE ###

R = input()

print( 2 * 3.141 * int(R) )
n,m = int(input().split())

print(n * (n-1) / 2 + m * (m-1) / 2)

### FIXED CODE ###

n,m = map(int, input().split())

print(int(n * (n-1) / 2 + m * (m-1) / 2))
def warshall_floyd(d, n):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


H, W = map(int, input().split())

fld = [""] * H
for i in range(H):
    fld[i] = input()

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

d = [[float("inf") for i in range(H * W+1)] for i in range(H * W+1)]
for i in range(H):
    for j in range(W):
        if fld[i][j] == '#':
            continue
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H or y < 0 or y >= W:
                continue
            if fld[x][y] == ".":
                d[i*H+j][x*H+y] = 1


for i in range(H*W):
    d[i][i] = 0

warshall = warshall_floyd(d, H*W)
ans = 0
for val in warshall:
    for v in val:
        if v != float("inf"):
            ans = max(ans, v)

print(ans)


### FIXED CODE ###

def warshall_floyd(d, n):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


H, W = map(int, input().split())

fld = [""] * H
for i in range(H):
    fld[i] = input()

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

d = [[float("inf") for i in range(H * W)] for i in range(H * W)]
for i in range(H):
    for j in range(W):
        if fld[i][j] == '#':
            continue
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H or y < 0 or y >= W:
                continue
            if fld[x][y] == ".":
                d[i*W+j][x*W+y] = 1


for i in range(H*W):
    d[i][i] = 0

warshall = warshall_floyd(d, H*W)
ans = 0
for val in warshall:
    for v in val:
        if v != float("inf"):
            ans = max(ans, v)

print(ans)
s=input()
t=input()
a=0
for i in len(s):
  if s[i]=!t[i]:
    a +=
print(a)
 


### FIXED CODE ###

s=input()
t=input()
a=0
for i in range(len(s)):
    if s[i]!=t[i]:
        a += 1
print(a)
print(~int(input())

### FIXED CODE ###

a = int(input())
if a == 0:
  print(1)
else:
  print(0)
N = int(input())
li = list(map(int,inpur().split()))

lis = sorted(li)
for i in range(N):
  if lis[-1] == li[i]:
    print(lis[-2])
  else: print(lis[-1])
    


### FIXED CODE ###

N = int(input())
li = [int(input()) for _ in range(N)]

lis = sorted(li)
for i in range(N):
  if lis[-1] == li[i]:
    print(lis[-2])
  else: print(lis[-1])
import itertools


def roll(dice, commands):
    for command in commands:
        if command == 'N':
            dice[0], dice[1], dice[5], dice[4] = dice[1], dice[5], dice[4], dice[0]
        elif command == 'W':
            dice[0], dice[3], dice[5], dice[2] = dice[2], dice[0], dice[3], dice[5]

A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(4):
    for j in range(4):
        for commands in itertools.permutations('N' * i + 'W' * j):
            a = A[::]
            roll(a, commands)
            if B == a:
                print('Yes')
                exit()
print('No')

### FIXED CODE ###

class Dice(list):
    def __init__(self, *args):
        super().__init__(*args)

    def roll(self, command):
        if command == 'N':
            self[0], self[1], self[5], self[4] = self[1], self[5], self[4], self[0]
        elif command == 'S':
            self[0], self[1], self[5], self[4] = self[4], self[0], self[1], self[5]
        elif command == 'W':
            self[0], self[3], self[5], self[2] = self[2], self[0], self[3], self[5]
        elif command == 'E':
            self[0], self[3], self[5], self[2] = self[3], self[5], self[2], self[0]

    def spin(self, repeat=1):
        for _ in range(repeat):
            self[3], self[1], self[2], self[4] = self[1], self[2], self[4], self[3]

    def _fix_position(self):
        self._min_num_to_top()
        self._min_side_num_to_front()

    def _min_num_to_top(self):
        idx = self.index(min(self))
        if idx == 0:
            pass
        elif idx == 1:
            self.roll('N')
        elif idx == 2:
            self.roll('W')
        elif idx == 3:
            self.roll('E')
        elif idx == 4:
            self.roll('S')
        elif idx == 5:
            self.roll('N')
            self.roll('N')

    def _min_side_num_to_front(self):
        idx = self.index(min(self[3], self[1], self[2], self[4]))
        if idx == 1:
            pass
        elif idx == 2:
            self.spin(1)
        elif idx == 3:
            self.spin(3)
        elif idx == 4:
            self.spin(2)

    def __hash__(self):
        self._fix_position()
        return hash(tuple(self))

    def __eq__(self, other):
        return hash(self) == hash(other)


if __name__ == '__main__':
    n = int(input())
    dices = set(Dice(map(int, input().split())) for _ in range(n))
    print('Yes' if n == len(dices) else 'No')
print((int(input())-1)*(int(input())-1))

### FIXED CODE ###

a, b = map(int, input().split())
print((a-1)*(b-1))
n, k = map(int, input().split())
s = input()
ans_0 = []
ans_1 = []
cnt = 0

if s.count("1") == 1:
    print(max(n - 1 - s.index("1"), s.index("1") + 1))
else:
    s = list(s)
    for i in range(n):
        if s[i] == "1" and cnt == 0:
            ans_0.append(i)
            cnt += 1
        elif s[i] == "0":
            cnt = 0
    cnt = 0
    for i in range(n):
        if s[n-1 - i] == "1" and cnt == 0:
            ans_1.append(n-1-i)
            cnt += 1
        elif s[n-1 - i] == "0":
            cnt = 0
    ans_1.sort()
    ans_list = []
    for i in range(len(ans_0) - k):
        ans_list.append(ans_1[i+k] - ans_0[i])
    print(max(ans_list) + 1)

### FIXED CODE ###

n, k = map(int, input().split())
s = list(input())
ans_0 = []
ans_1 = []

cnt = 0
for i in range(n):
    if s[i] == "1" and cnt == 0:
        ans_0.append(i)
        cnt += 1
    elif s[i] == "0":
        cnt = 0
cnt = 0
for i in range(n):
    if s[n-1 - i] == "1" and cnt == 0:
        ans_1.append(n-1-i)
        cnt += 1
    elif s[n-1 - i] == "0":
        cnt = 0
ans_1.sort()
ans_list = []
if len(ans_0) == 0:
    print(n)
elif len(ans_0) == 1:
    if k == 1:
        print(max(n-ans_0[0], ans_1[0]+1))
    else:
        print(n)
elif len(ans_0) < k:
    print(n)
else:
    if 0 not in ans_0:
        ans_0.append(0)
        ans_1.append(0)
        ans_0.sort()
        ans_1.sort()
    if n-1 not in ans_1:
        ans_0.append(n-1)
        ans_1.append(n-1)
    for i in range(len(ans_0) - k):
        ans_list.append(ans_1[i+k] - ans_0[i])
    print(max(ans_list) + 1)
w = input()

for i in w:
    if w.count(i)%2! = 0:
        ans = "No"
        break
else:
    ans = "Yes"

print(ans)


### FIXED CODE ###

w = input()

ans = "No"
for i in w:
    if w.count(i)%2!= 0:
        break
else:
    ans = "Yes"
print(ans)
from colletions import Counter

N = int(input())
d = Counter(map(int, input().split()))

sticks_len = []
for k, v in d:
  if v >= 2:
    sticks.append(k)
  if v >= 4:
    sticks.append(k)
    
sticks.sort()

if len(sticks) >= 2:
  print(sticks[-1] * sticks[-2]
else:
  print(0)

  

### FIXED CODE ###

from collections import Counter
 
N = int(input())
d = Counter(map(int, input().split()))
 
sticks_len = []
for k, v in d.items():
  if v >= 2:
    sticks_len.append(k)
  if v >= 4:
    sticks_len.append(k)
    
sticks_len.sort()
 
if len(sticks_len) >= 2:
  print(sticks_len[-1] * sticks_len[-2])
else:
  print(0)
n, tm = map(int, input().split())
ctl = []
for i in range(n):
    c, t = map(int, input().split())
    ctl.append([c, t])

filctl = []
for elm in ctl:
    if elm[1] <= tm:
        filctl.append(elm)

filctl = sorted(filctl)
print((filctl[0])[0])

### FIXED CODE ###

n, tm = map(int, input().split())
ctl = []
for i in range(n):
    c, t = map(int, input().split())
    ctl.append([c, t])

filctl = []
for elm in ctl:
    if elm[1] <= tm:
        filctl.append(elm)
if len(filctl) != 0:
    filctl = sorted(filctl)
    print((filctl[0])[0])
else:
    print("TLE")
from sys import stdin
n = int(stdin.readline().rstrip())
arr = stdin.readline().rstrip().split()
pre_num = arr.pop(0)
cnt = 0
max_cnt = 0
for i in arr:
  current_num = int(i)
  if pre_num >= current_num:
    cnt += 1
  else:
    if max_cnt < cnt:
      max_cnt = cnt
    cnt = 0
  pre_num = current_num
if max_cnt < cnt:
  max_cnt = cnt
print(max_cnt)


### FIXED CODE ###

from sys import stdin
n = int(stdin.readline().rstrip())
arr = stdin.readline().rstrip().split()
pre_num = int(arr.pop(0))
cnt = 0
max_cnt = 0
for i in arr:
  current_num = int(i)
  if pre_num >= current_num:
    cnt += 1
  else:
    if max_cnt < cnt:
      max_cnt = cnt
    cnt = 0
  pre_num = current_num
if max_cnt < cnt:
  max_cnt = cnt
print(max_cnt)
S = input()
N = len(S)
dp = [[0 for i in range(13)] for j in range(N+1)]

MOD = 10**9 + 7
dp[0][0] = 1

for i in range(1,N+1):
    if S[i-1] == "?":
        for j in range(13):
            for k in range(10):
                dp[i][(j*10+k)%13] += dp[i-1][j]
                dp[i][(j*10+k)%13] %= MOD
    else:
        t = int(S[i-1])
        for j in range(13):
            dp[i][(j*10+t)%13] += dp[i-1][j]
            dp[i][(j*10+k)%13] %= MOD

print(dp[N][5])


### FIXED CODE ###

S = input()
N = len(S)
dp = [[0 for i in range(13)] for j in range(N+1)]

MOD = 10**9 + 7
dp[0][0] = 1

for i in range(1,N+1):
    if S[i-1] == "?":
        for j in range(13):
            for k in range(10):
                dp[i][(j*10+k)%13] += dp[i-1][j]
                dp[i][(j*10+k)%13] %= MOD
    else:
        t = int(S[i-1])
        for j in range(13):
            dp[i][(j*10+t)%13] += dp[i-1][j]
            dp[i][(j*10+t)%13] %= MOD

print(dp[N][5])
N = int(input())

p = [0]*10**6
for i in range(2, 10**6):
    if p[i] == 0:
        p[i] = 1
        j = 2
        while i*j < 10**6:
            p[i*j] = -1
            j += 1

L = []
ans = 0
for i in range(10**6):
    if p[i] < 1:
        continue
    j = 1
    while N%pow(i, j) == 0:
        L.append(pow(i, j))
        N //= pow(i, j)
        j += 1
        ans += 1

L2 = []
i = 2
while i <= N**0.5:
    if N%i == 0:
        L2.append(N, N//i)
    i += 1

if len(L2) == 0 and N > 1 and N not in L:
    ans += 1

print(ans)

### FIXED CODE ###

N = int(input())

p = [0]*10**6
for i in range(2, 10**6):
    if p[i] == 0:
        p[i] = 1
        j = 2
        while i*j < 10**6:
            p[i*j] = -1
            j += 1

ans = 0
for i in range(10**6):
    if p[i] < 1:
        continue
    j = 1
    while N%pow(i, j) == 0:
        N //= pow(i, j)
        j += 1
        ans += 1

for i in range(10**6):
    if p[i] < 1:
        continue
    while N%i == 0:
        N //= i
    
if N > 10**6:
    i = 2
    f = True
    while i <= N**0.5:
        if N%i == 0:
            f = False
            break
        i += 1
    if f:
        ans += 1

print(ans)
a1,a2,b1,b2=map(int,input().split())
print(max(min(a2,b2)-max(b1,a1),0)


### FIXED CODE ###

a1,a2,b1,b2=map(int,input().split())
print(max(min(a2,b2)-max(b1,a1),0))
a = input()
b = a[0]
c = False
for i in range(len(a)):
    if a[i] != b:
        c = True
        print("Yes")
        return
print("No")

### FIXED CODE ###

a = input()
b = a[0]
c = False
for i in range(len(a)):
    if a[i] != b:
        c = True
if c:
    print("Yes")
else:
    print("No")
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
from collections import deque

H,W=map(int, sys.stdin.readline().split())
CH,CW=map(int, sys.stdin.readline().split())
CH-=1
CW-=1
DH,DW=map(int, sys.stdin.readline().split())
DH-=1
DW-=1
S=[ sys.stdin.readline().strip() for _ in range(W) ]

D=[(0,1),(0,-1),(1,0),(-1,0)]
WARP=[(1,1),(-1,1),(1,-1),(-1,-1),(2,0),(-2,0),(0,2),(0,-2),(1,2),(-1,2),(1,-2),(-1,-2),(2,1),(-2,1),(2,-1),(-2,-1),(2,2),(-2,2),(2,-2),(-2,-2)]

d=defaultdict(lambda: float("inf"))    #(h,w):スタート地点からその点までの距離
visit=defaultdict(lambda: 0) #未訪問:0 訪問済み:1

q=deque()
q.append((CH,CW))
d[(CH,CW)]=0

while q:
    fro_h, fro_w=q.popleft()
    visit[(fro_h, fro_w)]=1
    if fro_h==DH and fro_w==DW:
        print d[(DH,DW)]
        quit()
    #歩いて移動
    for dh,dw in D:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W:
            if S[to_h][to_w]==".":
                alt=d[(fro_h,fro_w)]+0
                if alt<d[(to_h,to_w)] and visit[(to_h, to_w)]==0:
                    d[(to_h, to_w)]=alt
                    q.appendleft((to_h, to_w))
    #ワープ
    for dh,dw in WARP:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W:
            if S[to_h][to_w]==".":
                alt=d[(fro_h,fro_w)]+1
                if alt<d[(to_h,to_w)] and visit[(to_h, to_w)]==0:
                    d[(to_h, to_w)]=alt
                    q.append((to_h, to_w))

print -1


### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque

H,W=map(int, sys.stdin.readline().split())
CH,CW=map(int, sys.stdin.readline().split())
CH-=1
CW-=1
DH,DW=map(int, sys.stdin.readline().split())
DH-=1
DW-=1
S=[ sys.stdin.readline().strip() for _ in range(H) ]

D=[(0,1),(0,-1),(1,0),(-1,0)]
WARP=[]
for dh in range(-2,3):
    for dw in range(-2,3):
        if abs(dh)+abs(dw)<=1: continue
        WARP.append((dh,dw))

d=defaultdict(lambda: float("inf")) #スタート地点から各マスまでの距離

q=deque()
q.append((0,CH,CW))
d[(CH,CW)]=0

while q:
    kyori, fro_h, fro_w=q.popleft()
    if d[(fro_h, fro_w)]<kyori: continue
    #歩いて移動
    for dh,dw in D:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":
            if kyori < d[(to_h, to_w)]:
                d[(to_h, to_w)]=kyori
                q.appendleft((kyori, to_h, to_w))
    #ワープ
    for dh,dw in WARP:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":
            if kyori+1 < d[(to_h, to_w)]:
                d[(to_h, to_w)]=kyori+1
                q.append((kyori+1, to_h, to_w))

if d[(DH,DW)]==float("inf"):
    print -1
else:
    print d[(DH,DW)]
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 10**9+7
for i in range(2**N):
    card = [[],[]]
    num = []
    x = i
    for j in range(N):
        if i&1:
            num.append(B[j])
            card[(j+1)%2].append(j)
        else:
            num.append(A[j])
            card[(j)%2].append(j)
        i >>= 1
    if len(card[1]) != N//2:
        continue
    card[0].sort(key=lambda x:[num[x],x]);card[1].sort(key=lambda x:[num[x],x])
    hoge = []
    for i in range(N):
        hoge.append(card[i%2][i//2])
    flag = True
    for i in range(N-1):
        if num[hoge[i]] > num[hoge[i+1]]:
            flag = False
    if flag:
        huga = sum(abs(hoge[i]-i)for i in range(N))
        if huga % 2:
            print(1/0)
        else:
            huge //= 2
        if huga < ans:
            ans = huga
if ans >= 10**9:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

import bisect
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 10**9+7
for i in range(2**N):
    card = [[],[]]
    num = []
    iii = i
    for j in range(N):
        if i&1:
            num.append(B[j])
            card[(j+1)%2].append(j)
        else:
            num.append(A[j])
            card[(j)%2].append(j)
        i >>= 1
    if len(card[1]) != N//2:
        continue
    card[0].sort(key=lambda x:[num[x],x])
    card[1].sort(key=lambda x:[num[x],x])
    hoge = []
    for i in range(N):
        hoge.append(card[i%2][i//2])
    flag = True
    for i in range(N-1):
        if num[hoge[i]] > num[hoge[i+1]]:
            flag = False
    if flag:
        huga = []
        count = 0
        for i in range(N):
            l = bisect.bisect_left(huga,hoge[i])
            huga.insert(l,hoge[i])
            count += i-l
        if count < ans:
            ans = count
if ans >= 10**9:
    print(-1)
else:
    print(ans)