# https://atcoder.jp/contests/abc150/submissions/9394047



from itertools import permutations



N = int(eval(input()))

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

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



perms = tuple(permutations(list(range(1, N + 1))))

p = perms.index(P)

q = perms.index(Q)

print((abs(p - q)))

from itertools import permutations



n = int(eval(input()))

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

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



pi, qi = -1, -1

for i, perm in enumerate(permutations(list(range(1, n + 1)))):

    if perm == p:

        pi = i

        if qi != -1:

            break

    if perm == q:

        qi = i

        if pi != -1:

            break

print((abs(pi - qi)))

# https://atcoder.jp/contests/abc150/submissions/9394047
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
perms = tuple(permutations(list(range(1, N + 1))))
p = perms.index(P)
q = perms.index(Q)
print((abs(p - q)))

from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
pi, qi = -1, -1
for i, perm in enumerate(permutations(list(range(1, n + 1)))):
    if perm == p:
        pi = i
        if qi != -1:
            break
    if perm == q:
        qi = i
        if pi != -1:
            break
print((abs(pi - qi)))

-# https://atcoder.jp/contests/abc150/submissions/9394047 -N = int(eval(input())) -P = tuple(map(int, input().split())) -Q = tuple(map(int, input().split())) -perms = tuple(permutations(list(range(1, N + 1)))) -p = perms.index(P) -q = perms.index(Q) -print((abs(p - q))) +n = int(eval(input())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +pi, qi = -1, -1 +for i, perm in enumerate(permutations(list(range(1, n + 1)))): +    if perm == p: +        pi = i +        if qi != -1: +            break +    if perm == q: +        qi = i +        if pi != -1: +            break +print((abs(pi - qi)))