from itertools import permutations

n=int(eval(input()))

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

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

c=sorted(list(permutations(list(range(1,n+1)))))

a,b,=0,0

for i,c in enumerate(c):

    if p==list(c):

        a=i

    if q==list(c):

        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()))

P=sorted(list(permutations(list(range(1,n+1)),n)))

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

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
c = sorted(list(permutations(list(range(1, n + 1)))))
a, b, = (
    0,
    0,
)
for i, c in enumerate(c):
    if p == list(c):
        a = i
    if q == list(c):
        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()))
P = sorted(list(permutations(list(range(1, n + 1)), n)))
print((abs(P.index(p) - P.index(q))))

-p = list(map(int, input().split())) -q = list(map(int, input().split())) -c = sorted(list(permutations(list(range(1, n + 1))))) -a, b, = ( -    0, -    0, -) -for i, c in enumerate(c): -    if p == list(c): -        a = i -    if q == list(c): -        b = i -print((abs(a - b))) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +P = sorted(list(permutations(list(range(1, n + 1)), n))) +print((abs(P.index(p) - P.index(q))))