from itertools import permutations



N = int(eval(input()))

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

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



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

#print("A:", A)



print((abs((A.index(P) - A.index(Q)))))
from itertools import permutations



N = int(eval(input()))

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

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



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

print((abs(D.index(P) - D.index(Q))))
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = list(permutations(list(range(1, N + 1))))
# print("A:", A)
print((abs((A.index(P) - A.index(Q)))))

from itertools import permutations

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

-A = list(permutations(list(range(1, N + 1)))) -# print("A:", A) -print((abs((A.index(P) - A.index(Q))))) +D = list(permutations(list(range(1, N + 1)))) +print((abs(D.index(P) - D.index(Q))))