from itertools import permutations

n = int(eval(input()))

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

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

s = "".join(map(str, list(range(1, (n+1)))))

x = list(permutations(s))

x = [list(i) for i in x]

print((abs(x.index(p)-x.index(q))))

from itertools import permutations



N = int(eval(input()))

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

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



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

    if P == X:

        a = i

    if Q == X:

        b = i

print((abs(a - b)))

from itertools import permutations

n = int(eval(input()))
p = list(map(str, input().split()))
q = list(map(str, input().split()))
s = "".join(map(str, list(range(1, (n + 1)))))
x = list(permutations(s))
x = [list(i) for i in x]
print((abs(x.index(p) - x.index(q))))

from itertools import permutations

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

-n = int(eval(input())) -p = list(map(str, input().split())) -q = list(map(str, input().split())) -s = "".join(map(str, list(range(1, (n + 1))))) -x = list(permutations(s)) -x = [list(i) for i in x] -print((abs(x.index(p) - x.index(q)))) +N = int(eval(input())) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +for i, X in enumerate(permutations(list(range(1, N + 1)), N)): +    if P == X: +        a = i +    if Q == X: +        b = i +print((abs(a - b)))