from itertools import permutations

N = int(eval(input()))

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

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

A = sorted(P)

cnt = 0

for x in permutations(A,N):

    x = list(x)

    if x==P:

        a = cnt

    if x==Q:

        b = cnt

    cnt += 1

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

N = int(eval(input()))

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

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

cnt = 0

for x in permutations(list(range(1,N+1)),N):

    if P==x:

        indP = cnt

    if Q==x:

        indQ = cnt

    cnt += 1

print((abs(indP-indQ)))
from itertools import permutations

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
A = sorted(P)
cnt = 0
for x in permutations(A, N):
    x = list(x)
    if x == P:
        a = cnt
    if x == Q:
        b = cnt
    cnt += 1
print((abs(a - b)))

from itertools import permutations

N = int(eval(input()))
P = tuple(list(map(int, input().split())))
Q = tuple(list(map(int, input().split())))
cnt = 0
for x in permutations(list(range(1, N + 1)), N):
    if P == x:
        indP = cnt
    if Q == x:
        indQ = cnt
    cnt += 1
print((abs(indP - indQ)))

-P = list(map(int, input().split())) -Q = list(map(int, input().split())) -A = sorted(P) +P = tuple(list(map(int, input().split()))) +Q = tuple(list(map(int, input().split()))) -for x in permutations(A, N): -    x = list(x) -    if x == P: -        a = cnt -    if x == Q: -        b = cnt +for x in permutations(list(range(1, N + 1)), N): +    if P == x: +        indP = cnt +    if Q == x: +        indQ = cnt -print((abs(a - b))) +print((abs(indP - indQ)))