import itertools

import math

N = int(eval(input()))

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

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

L = list(map(list,list(itertools.permutations(list(i for i in range(1,N+1)),N))))

a = L.index(P)

b = L.index(Q)

print((abs(a-b)))
from itertools import permutations 



N = int(eval(input()))

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

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



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

print((abs(perm.index(P) - perm.index(Q))))
import itertools
import math

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = list(map(list, list(itertools.permutations(list(i for i in range(1, N + 1)), N))))
a = L.index(P)
b = L.index(Q)
print((abs(a - b)))

from itertools import permutations

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

-import itertools -import math +from itertools import permutations -P = list(map(int, input().split())) -Q = list(map(int, input().split())) -L = list(map(list, list(itertools.permutations(list(i for i in range(1, N + 1)), N)))) -a = L.index(P) -b = L.index(Q) -print((abs(a - b))) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +perm = list(permutations(list(range(1, N + 1)), N)) +print((abs(perm.index(P) - perm.index(Q))))