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

05a97215d4f18112f9488a5c0006c70b13b06e28
<mask token>

<mask token>
print(abs(A.index(tuple(P)) - A.index(tuple(Q))))

<mask token>
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
A = list(itertools.permutations(list(range(1, N + 1))))
print(abs(A.index(tuple(P)) - A.index(tuple(Q))))

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