from itertools import permutations



N = int(eval(input()))

P = tuple(int(x) for x in input().split())

Q = tuple(int(x) for x in input().split())



ps = list(permutations(list(range(1, N + 1))))

ps.index(P)



print((abs(ps.index(Q) - ps.index(P))))

from itertools import permutations



N = int(eval(input()))

P = tuple(int(x) for x in input().split())

Q = tuple(int(x) for x in input().split())



ps = permutations(list(range(1, N + 1)))



try:

    x, y = [i for i, t in enumerate(ps) if t == P or t == Q]

except ValueError:

    print((0))

else:

    print((abs(x - y)))

from itertools import permutations

N = int(eval(input()))
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
ps = list(permutations(list(range(1, N + 1))))
ps.index(P)
print((abs(ps.index(Q) - ps.index(P))))

from itertools import permutations

N = int(eval(input()))
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
ps = permutations(list(range(1, N + 1)))
try:
    x, y = [i for i, t in enumerate(ps) if t == P or t == Q]
except ValueError:
    print((0))
else:
    print((abs(x - y)))

-ps = list(permutations(list(range(1, N + 1)))) -ps.index(P) -print((abs(ps.index(Q) - ps.index(P)))) +ps = permutations(list(range(1, N + 1))) +try: +    x, y = [i for i, t in enumerate(ps) if t == P or t == Q] +except ValueError: +    print((0)) +else: +    print((abs(x - y)))