def dfs(res, chk):

    if len(res) == n:

        global idx

        global pi

        global qi

        idx += 1

        pb = all(res[i]+1 == p for i, p in enumerate(P))

        qb = all(res[i]+1 == q for i, q in enumerate(Q))

        if pb:

            pi = idx

        if qb:

            qi = idx

        return

    

    for i in range(n):

        if chk & (1 << i):

            continue

        else:

            dfs(res + [i], chk^(1 << i))



n = int(eval(input()))

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

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

idx = 0

pi = qi = 0

perm = []

dfs([], 0)

print((abs(pi-qi)))
# AtCoder Beginner Contest 150

# C - Count Order

# https://atcoder.jp/contests/abc150/tasks/abc150_c



from itertools import permutations



N = int(eval(input()))

P = tuple(i-1 for i in map(int, input().split()))

Q = tuple(i-1 for i in map(int, input().split()))



F = list(permutations(list(range(N))))



print((abs(F.index(P) - F.index(Q))))

def dfs(res, chk):
    if len(res) == n:
        global idx
        global pi
        global qi
        idx += 1
        pb = all(res[i] + 1 == p for i, p in enumerate(P))
        qb = all(res[i] + 1 == q for i, q in enumerate(Q))
        if pb:
            pi = idx
        if qb:
            qi = idx
        return
    for i in range(n):
        if chk & (1 << i):
            continue
        else:
            dfs(res + [i], chk ^ (1 << i))


n = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
idx = 0
pi = qi = 0
perm = []
dfs([], 0)
print((abs(pi - qi)))

# AtCoder Beginner Contest 150
# C - Count Order
# https://atcoder.jp/contests/abc150/tasks/abc150_c
from itertools import permutations

N = int(eval(input()))
P = tuple(i - 1 for i in map(int, input().split()))
Q = tuple(i - 1 for i in map(int, input().split()))
F = list(permutations(list(range(N))))
print((abs(F.index(P) - F.index(Q))))

-def dfs(res, chk): -    if len(res) == n: -        global idx -        global pi -        global qi -        idx += 1 -        pb = all(res[i] + 1 == p for i, p in enumerate(P)) -        qb = all(res[i] + 1 == q for i, q in enumerate(Q)) -        if pb: -            pi = idx -        if qb: -            qi = idx -        return -    for i in range(n): -        if chk & (1 << i): -            continue -        else: -            dfs(res + [i], chk ^ (1 << i)) +# AtCoder Beginner Contest 150 +# C - Count Order +# https://atcoder.jp/contests/abc150/tasks/abc150_c +from itertools import permutations - -n = int(eval(input())) -P = list(map(int, input().split())) -Q = list(map(int, input().split())) -idx = 0 -pi = qi = 0 -perm = [] -dfs([], 0) -print((abs(pi - qi))) +N = int(eval(input())) +P = tuple(i - 1 for i in map(int, input().split())) +Q = tuple(i - 1 for i in map(int, input().split())) +F = list(permutations(list(range(N)))) +print((abs(F.index(P) - F.index(Q))))