from itertools import permutations



n = int(input())

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

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



s = list(range(1, n+1))

s = list(permutations(s))



a, b = 0, 0



for i, v in enumerate(s, start=1):

	if v == p:

		a = i

	elif v == q:

		b = i

	else:

		pass



print(0) if p == q else print(abs(a - b))

n = int(eval(input()))

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

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



from itertools import permutations



l = list(range(1, n + 1))

perm = permutations(l, n)



idx_P, idx_Q = 0, 0

for i,v in enumerate(perm):

    if v == tuple(P):

        idx_P = i

    if v == tuple(Q):

        idx_Q = i



print((abs(idx_P - idx_Q)))
from itertools import permutations

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
s = list(range(1, n + 1))
s = list(permutations(s))
a, b = 0, 0
for i, v in enumerate(s, start=1):
    if v == p:
        a = i
    elif v == q:
        b = i
    else:
        pass
print(0) if p == q else print(abs(a - b))

n = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
from itertools import permutations

l = list(range(1, n + 1))
perm = permutations(l, n)
idx_P, idx_Q = 0, 0
for i, v in enumerate(perm):
    if v == tuple(P):
        idx_P = i
    if v == tuple(Q):
        idx_Q = i
print((abs(idx_P - idx_Q)))

+n = int(eval(input())) +P = list(map(int, input().split())) +Q = list(map(int, input().split())) -n = int(input()) -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -s = list(range(1, n + 1)) -s = list(permutations(s)) -a, b = 0, 0 -for i, v in enumerate(s, start=1): -    if v == p: -        a = i -    elif v == q: -        b = i -    else: -        pass -print(0) if p == q else print(abs(a - b)) +l = list(range(1, n + 1)) +perm = permutations(l, n) +idx_P, idx_Q = 0, 0 +for i, v in enumerate(perm): +    if v == tuple(P): +        idx_P = i +    if v == tuple(Q): +        idx_Q = i +print((abs(idx_P - idx_Q)))