from itertools import permutations



N = int(eval(input()))

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

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



P = []

for perm in permutations(list(range(1, N + 1)), r=N):

    P.append(tuple(perm))



P.sort()

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



N = int(eval(input()))

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

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



X = [tuple(p) for p in permutations(list(range(1, N + 1)), r=N)]

X.sort()



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

from itertools import permutations

N = int(eval(input()))
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
P = []
for perm in permutations(list(range(1, N + 1)), r=N):
    P.append(tuple(perm))
P.sort()
print((abs(P.index(A) - P.index(B))))

from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
X = [tuple(p) for p in permutations(list(range(1, N + 1)), r=N)]
X.sort()
print((abs(X.index(P) - X.index(Q))))

-A = tuple(map(int, input().split())) -B = tuple(map(int, input().split())) -P = [] -for perm in permutations(list(range(1, N + 1)), r=N): -    P.append(tuple(perm)) -P.sort() -print((abs(P.index(A) - P.index(B)))) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +X = [tuple(p) for p in permutations(list(range(1, N + 1)), r=N)] +X.sort() +print((abs(X.index(P) - X.index(Q))))