from itertools import permutations



N = int(eval(input()))

P = "".join(list(input().split()))

Q = "".join(list(input().split()))

#print("P:", P)



A = ""

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

  A += str(i)

A = list(permutations(A, N))

A.sort()

#print("A:", A)



B = []

for i in range(len(A)):

  B.append("".join(A[i]))



for i in range(len(B)):

  if P == B[i]:

    a = i

  if Q == B[i]:

    b = i

    

print((abs(a-b)))
from itertools import permutations



N = int(eval(input()))

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

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



A = list(permutations(list(range(1, N + 1))))

#print("A:", A)



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

N = int(eval(input()))
P = "".join(list(input().split()))
Q = "".join(list(input().split()))
# print("P:", P)
A = ""
for i in range(1, N + 1):
    A += str(i)
A = list(permutations(A, N))
A.sort()
# print("A:", A)
B = []
for i in range(len(A)):
    B.append("".join(A[i]))
for i in range(len(B)):
    if P == B[i]:
        a = i
    if Q == B[i]:
        b = i
print((abs(a - b)))

from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = list(permutations(list(range(1, N + 1))))
# print("A:", A)
print((abs((A.index(P) - A.index(Q)))))

-P = "".join(list(input().split())) -Q = "".join(list(input().split())) -# print("P:", P) -A = "" -for i in range(1, N + 1): -    A += str(i) -A = list(permutations(A, N)) -A.sort() +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +A = list(permutations(list(range(1, N + 1)))) -B = [] -for i in range(len(A)): -    B.append("".join(A[i])) -for i in range(len(B)): -    if P == B[i]: -        a = i -    if Q == B[i]: -        b = i -print((abs(a - b))) +print((abs((A.index(P) - A.index(Q)))))