from itertools import permutations

n = int(eval(input()))

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

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

perm = list(permutations(list(range(1, n + 1)), n))

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

n = int(eval(input()))

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

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

np = list(permutations(list(range(1, n + 1)), n))

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

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
perm = list(permutations(list(range(1, n + 1)), n))
print((abs(perm.index(p) - perm.index(q))))

from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
np = list(permutations(list(range(1, n + 1)), n))
print((abs(np.index(p) - np.index(q))))

-perm = list(permutations(list(range(1, n + 1)), n)) -print((abs(perm.index(p) - perm.index(q)))) +np = list(permutations(list(range(1, n + 1)), n)) +print((abs(np.index(p) - np.index(q))))