N = int(eval(input()))

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

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





import itertools



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



a = b = 1



for l in L:

    if l == P:

        break

    a += 1

for l in L:

    if l == Q:

        break

    b += 1

print((abs(a-b)))
n = int(eval(input()))

from itertools import *

l = list(permutations(list(range(1, n + 1)), n))

l.sort()

p = l.index(tuple(map(int, input().split())))

q = l.index(tuple(map(int, input().split())))

print((abs(p - q)))

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

L = list(itertools.permutations([i for i in range(1, N + 1)]))
a = b = 1
for l in L:
    if l == P:
        break
    a += 1
for l in L:
    if l == Q:
        break
    b += 1
print((abs(a - b)))

n = int(eval(input()))
from itertools import *

l = list(permutations(list(range(1, n + 1)), n))
l.sort()
p = l.index(tuple(map(int, input().split())))
q = l.index(tuple(map(int, input().split())))
print((abs(p - q)))

-N = int(eval(input())) -P = tuple(map(int, input().split())) -Q = tuple(map(int, input().split())) -import itertools +n = int(eval(input())) +from itertools import * -L = list(itertools.permutations([i for i in range(1, N + 1)])) -a = b = 1 -for l in L: -    if l == P: -        break -    a += 1 -for l in L: -    if l == Q: -        break -    b += 1 -print((abs(a - b))) +l = list(permutations(list(range(1, n + 1)), n)) +l.sort() +p = l.index(tuple(map(int, input().split()))) +q = l.index(tuple(map(int, input().split()))) +print((abs(p - q)))