import itertools



N = int(eval(input()))

T = list(range(1,N+1))

S = list(itertools.permutations(T, N))

P = [int(x) for x in input().split()]

Q = [int(x) for x in input().split()]



for i in range(len(S)):

    if tuple(P) == S[i]:

        A = i

    if tuple(Q) == S[i]:

        B = i

print((abs(A-B)))
import itertools



N = int(eval(input()))

T = list(range(1,N+1))

S = list(itertools.permutations(T, N))

P = [int(x) for x in input().split()]

Q = [int(x) for x in input().split()]



A = S.index(tuple(P))

B = S.index(tuple(Q))

print((abs(A-B)))
import itertools

N = int(eval(input()))
T = list(range(1, N + 1))
S = list(itertools.permutations(T, N))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
for i in range(len(S)):
    if tuple(P) == S[i]:
        A = i
    if tuple(Q) == S[i]:
        B = i
print((abs(A - B)))

import itertools

N = int(eval(input()))
T = list(range(1, N + 1))
S = list(itertools.permutations(T, N))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
A = S.index(tuple(P))
B = S.index(tuple(Q))
print((abs(A - B)))

-for i in range(len(S)): -    if tuple(P) == S[i]: -        A = i -    if tuple(Q) == S[i]: -        B = i +A = S.index(tuple(P)) +B = S.index(tuple(Q))