import itertools

n = int(eval(input()))

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

pp = "".join((str(n) for n in p))

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

qq = "".join((str(n) for n in q))

a = [i+1 for i in range(n)]

b = 0

c = 0

d = 0

for i in itertools.permutations(a):

	d += 1

	x = "".join((str(n) for n in i))

	if pp == x:

		b = d

	if qq == x:

		c = d

print((abs(b-c)))
import itertools

n = int(eval(input()))

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

pp = "".join([str(i) for i in p])

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

qq = "".join([str(i) for i in q])

x = [i+1 for i in range(n)]

z = []

for v in itertools.permutations(x):

	y = "".join([str(i) for i in v])

	z.append(y)

a = z.index(pp)

b = z.index(qq)

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

n = int(eval(input()))
p = list(map(int, input().split()))
pp = "".join((str(n) for n in p))
q = list(map(int, input().split()))
qq = "".join((str(n) for n in q))
a = [i + 1 for i in range(n)]
b = 0
c = 0
d = 0
for i in itertools.permutations(a):
    d += 1
    x = "".join((str(n) for n in i))
    if pp == x:
        b = d
    if qq == x:
        c = d
print((abs(b - c)))

import itertools

n = int(eval(input()))
p = list(map(int, input().split()))
pp = "".join([str(i) for i in p])
q = list(map(int, input().split()))
qq = "".join([str(i) for i in q])
x = [i + 1 for i in range(n)]
z = []
for v in itertools.permutations(x):
    y = "".join([str(i) for i in v])
    z.append(y)
a = z.index(pp)
b = z.index(qq)
print((abs(a - b)))

-pp = "".join((str(n) for n in p)) +pp = "".join([str(i) for i in p]) -qq = "".join((str(n) for n in q)) -a = [i + 1 for i in range(n)] -b = 0 -c = 0 -d = 0 -for i in itertools.permutations(a): -    d += 1 -    x = "".join((str(n) for n in i)) -    if pp == x: -        b = d -    if qq == x: -        c = d -print((abs(b - c))) +qq = "".join([str(i) for i in q]) +x = [i + 1 for i in range(n)] +z = [] +for v in itertools.permutations(x): +    y = "".join([str(i) for i in v]) +    z.append(y) +a = z.index(pp) +b = z.index(qq) +print((abs(a - b)))