from itertools import permutations

n=int(eval(input()))

p="".join(input().split())

q="".join(input().split())

N=""

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

  N += str(i)

perms = list(permutations(N,n))

for i in range(len(perms)):

  perms[i] = "".join(list(perms[i]))



print((abs(perms.index(p) - perms.index(q))))
from itertools import permutations

N=int(eval(input()))

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

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

P=tuple([x-1 for x in P])

Q=tuple([x-1 for x in Q])

l=list(permutations(list(range(N))))

print((abs(l.index(P)-l.index(Q))))
from itertools import permutations

n = int(eval(input()))
p = "".join(input().split())
q = "".join(input().split())
N = ""
for i in range(1, n + 1):
    N += str(i)
perms = list(permutations(N, n))
for i in range(len(perms)):
    perms[i] = "".join(list(perms[i]))
print((abs(perms.index(p) - perms.index(q))))

from itertools import permutations

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P = tuple([x - 1 for x in P])
Q = tuple([x - 1 for x in Q])
l = list(permutations(list(range(N))))
print((abs(l.index(P) - l.index(Q))))

-n = int(eval(input())) -p = "".join(input().split()) -q = "".join(input().split()) -N = "" -for i in range(1, n + 1): -    N += str(i) -perms = list(permutations(N, n)) -for i in range(len(perms)): -    perms[i] = "".join(list(perms[i])) -print((abs(perms.index(p) - perms.index(q)))) +N = int(eval(input())) +P = list(map(int, input().split())) +Q = list(map(int, input().split())) +P = tuple([x - 1 for x in P]) +Q = tuple([x - 1 for x in Q]) +l = list(permutations(list(range(N)))) +print((abs(l.index(P) - l.index(Q))))