import itertools as it

N = int(eval(input()))

Q = []

P = []

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

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

Jun = []

box = [i+1 for i in range(N)]



for num in it.permutations(Q):

    num = list(num)

    Jun.append(num)

Jun = sorted(Jun)

for i, num in enumerate(Jun):

    if Q == num:

        a = i

    if P == num:

        b = i

print((abs(a-b)))



import itertools as it

N = int(eval(input()))

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

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

a = 0

b = 0

array = list(it.permutations(i for i in range(1,N+1)))

for i,ar in enumerate(array):

    if ar == P:

        a = i

    if ar == Q:

        b = i

print((abs(a-b)))
import itertools as it

N = int(eval(input()))
Q = []
P = []
Q = list(map(int, input().split()))
P = list(map(int, input().split()))
Jun = []
box = [i + 1 for i in range(N)]
for num in it.permutations(Q):
    num = list(num)
    Jun.append(num)
Jun = sorted(Jun)
for i, num in enumerate(Jun):
    if Q == num:
        a = i
    if P == num:
        b = i
print((abs(a - b)))

import itertools as it

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = 0
b = 0
array = list(it.permutations(i for i in range(1, N + 1)))
for i, ar in enumerate(array):
    if ar == P:
        a = i
    if ar == Q:
        b = i
print((abs(a - b)))

-Q = [] -P = [] -Q = list(map(int, input().split())) -P = list(map(int, input().split())) -Jun = [] -box = [i + 1 for i in range(N)] -for num in it.permutations(Q): -    num = list(num) -    Jun.append(num) -Jun = sorted(Jun) -for i, num in enumerate(Jun): -    if Q == num: +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +a = 0 +b = 0 +array = list(it.permutations(i for i in range(1, N + 1))) +for i, ar in enumerate(array): +    if ar == P: -    if P == num: +    if ar == Q: