import sys

import itertools

import math

#import numpy as np



stdin = sys.stdin



ri = lambda: int(rs())

rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only

rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces



N = ri()

P = rl()

Q = rl()

L = []

for x in itertools.permutations(list(range(1, N+1))):

    L.append(list(x))

a = 0

b = 0

for i in range(math.factorial(N)):

    if L[i] == P:

        a = i

    if L[i] == Q:

        b = i



print((abs(a-b)))

import sys

import itertools



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



N = ir()

P = tuple(lr())

Q = tuple(lr())

a = -1; b = -1

for i, x in enumerate(itertools.permutations(list(range(1, N+1)), N)):

    if x == P:

        a = i

    if x == Q:

        b = i



answer = abs(a-b)

print(answer)

import sys
import itertools
import math

# import numpy as np
stdin = sys.stdin
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces
N = ri()
P = rl()
Q = rl()
L = []
for x in itertools.permutations(list(range(1, N + 1))):
    L.append(list(x))
a = 0
b = 0
for i in range(math.factorial(N)):
    if L[i] == P:
        a = i
    if L[i] == Q:
        b = i
print((abs(a - b)))

import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N = ir()
P = tuple(lr())
Q = tuple(lr())
a = -1
b = -1
for i, x in enumerate(itertools.permutations(list(range(1, N + 1)), N)):
    if x == P:
        a = i
    if x == Q:
        b = i
answer = abs(a - b)
print(answer)

-import math -# import numpy as np -stdin = sys.stdin -ri = lambda: int(rs()) -rl = lambda: list(map(int, stdin.readline().split()))  # applies to numbers only -rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces -N = ri() -P = rl() -Q = rl() -L = [] -for x in itertools.permutations(list(range(1, N + 1))): -    L.append(list(x)) -a = 0 -b = 0 -for i in range(math.factorial(N)): -    if L[i] == P: +sr = lambda: sys.stdin.readline().rstrip() +ir = lambda: int(sr()) +lr = lambda: list(map(int, sr().split())) +N = ir() +P = tuple(lr()) +Q = tuple(lr()) +a = -1 +b = -1 +for i, x in enumerate(itertools.permutations(list(range(1, N + 1)), N)): +    if x == P: -    if L[i] == Q: +    if x == Q: -print((abs(a - b))) +answer = abs(a - b) +print(answer)