from itertools import permutations



N = int(eval(input()))

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

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



a = 0

b = 0

for i, v in enumerate(permutations(list(range(1, N + 1)), N)):

    if v == P:

        a = i

    if v == Q:

        b = i

print((abs(a - b)))



N = int(eval(input()))

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

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





def dfs(x, d, res):

    if len(x) == N + 1:

        d[0] += 1

        if x[1:] == P:

            res["P"] = d[0]

        if x[1:] == Q:

            res["Q"] = d[0]

        return



    for v in range(1, N + 1):

        if v not in x:

            x.append(v)

            dfs(x, d, res)

            x.pop()



res = {"P": 0, "Q": 0}

dfs([0], [0], res)

print((abs(res["P"] - res["Q"])))

from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = 0
b = 0
for i, v in enumerate(permutations(list(range(1, N + 1)), N)):
    if v == P:
        a = i
    if v == Q:
        b = i
print((abs(a - b)))

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))


def dfs(x, d, res):
    if len(x) == N + 1:
        d[0] += 1
        if x[1:] == P:
            res["P"] = d[0]
        if x[1:] == Q:
            res["Q"] = d[0]
        return
    for v in range(1, N + 1):
        if v not in x:
            x.append(v)
            dfs(x, d, res)
            x.pop()


res = {"P": 0, "Q": 0}
dfs([0], [0], res)
print((abs(res["P"] - res["Q"])))

-from itertools import permutations +N = int(eval(input())) +P = list(map(int, input().split())) +Q = list(map(int, input().split())) -N = int(eval(input())) -P = tuple(map(int, input().split())) -Q = tuple(map(int, input().split())) -a = 0 -b = 0 -for i, v in enumerate(permutations(list(range(1, N + 1)), N)): -    if v == P: -        a = i -    if v == Q: -        b = i -print((abs(a - b))) + +def dfs(x, d, res): +    if len(x) == N + 1: +        d[0] += 1 +        if x[1:] == P: +            res["P"] = d[0] +        if x[1:] == Q: +            res["Q"] = d[0] +        return +    for v in range(1, N + 1): +        if v not in x: +            x.append(v) +            dfs(x, d, res) +            x.pop() + + +res = {"P": 0, "Q": 0} +dfs([0], [0], res) +print((abs(res["P"] - res["Q"])))