from itertools import permutations



N = int(eval(input()))

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

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



for i, Rs in enumerate(permutations(list(range(1, N+1))), 1):

    Rs = list(Rs)

    if Ps == Rs:

        a = i

    if Qs == Rs:

        b = i



print((abs(a-b)))

from itertools import permutations



N = int(eval(input()))

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

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



noP, noQ = 0, 0

for no, Rs in enumerate(permutations(list(range(1, N+1)), r=N), start=1):

    if Ps == Rs:

        noP = no

    if Qs == Rs:

        noQ = no



print((abs(noP-noQ)))

from itertools import permutations

N = int(eval(input()))
Ps = list(map(int, input().split()))
Qs = list(map(int, input().split()))
for i, Rs in enumerate(permutations(list(range(1, N + 1))), 1):
    Rs = list(Rs)
    if Ps == Rs:
        a = i
    if Qs == Rs:
        b = i
print((abs(a - b)))

from itertools import permutations

N = int(eval(input()))
Ps = tuple(map(int, input().split()))
Qs = tuple(map(int, input().split()))
noP, noQ = 0, 0
for no, Rs in enumerate(permutations(list(range(1, N + 1)), r=N), start=1):
    if Ps == Rs:
        noP = no
    if Qs == Rs:
        noQ = no
print((abs(noP - noQ)))

-Ps = list(map(int, input().split())) -Qs = list(map(int, input().split())) -for i, Rs in enumerate(permutations(list(range(1, N + 1))), 1): -    Rs = list(Rs) +Ps = tuple(map(int, input().split())) +Qs = tuple(map(int, input().split())) +noP, noQ = 0, 0 +for no, Rs in enumerate(permutations(list(range(1, N + 1)), r=N), start=1): -        a = i +        noP = no -        b = i -print((abs(a - b))) +        noQ = no +print((abs(noP - noQ)))