N = int(eval(input()))

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

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



import itertools



A = [i for i in range(1, N+1)]



T = [list(i) for i in list(itertools.permutations(A, N))]

# print(T)

n1 = T.index(P)

n2 = T.index(Q)

print((abs(n1-n2)))
N = int(eval(input()))

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

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



from itertools import permutations



t = [i for i in range(1, N+1)]

r = list(permutations(t))



# print(P)

# print(Q)



i1 = r.index(P)

i2 = r.index(Q)

print((abs(i1-i2)))
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
import itertools

A = [i for i in range(1, N + 1)]
T = [list(i) for i in list(itertools.permutations(A, N))]
# print(T)
n1 = T.index(P)
n2 = T.index(Q)
print((abs(n1 - n2)))

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
from itertools import permutations

t = [i for i in range(1, N + 1)]
r = list(permutations(t))
# print(P)
# print(Q)
i1 = r.index(P)
i2 = r.index(Q)
print((abs(i1 - i2)))

-P = list(map(int, input().split())) -Q = list(map(int, input().split())) -import itertools +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +from itertools import permutations -A = [i for i in range(1, N + 1)] -T = [list(i) for i in list(itertools.permutations(A, N))] -# print(T) -n1 = T.index(P) -n2 = T.index(Q) -print((abs(n1 - n2))) +t = [i for i in range(1, N + 1)] +r = list(permutations(t)) +# print(P) +# print(Q) +i1 = r.index(P) +i2 = r.index(Q) +print((abs(i1 - i2)))