from collections import deque



N = int(eval(input()))

P = [int(x) for x in input().split()]

Q = [int(x) for x in input().split()]



st = deque([[i] for i in range(N, 0, -1)])

ord = 0

a = 0

b = 0

while(len(st) > 0):

    if a > 0 and b > 0: break

    now = st.pop()

    if len(now) == N:

        ord += 1

    if P == now:

        a = ord

    if Q == now:

        b = ord

    for i in range(N, 0, -1):

        if i in now: continue

        st.append(now + [i])



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



N = int(eval(input()))

P = tuple(int(x) for x in input().split())

Q = tuple(int(x) for x in input().split())



perm = list(permutations(list(range(1, N + 1)), N))

i = 0

a = 0; b = 0;

for i in range(len(perm)):

    if a > 0 and b > 0: break

    if perm[i] == P:  a = i + 1

    if perm[i] == Q:  b = i + 1



print((abs(b - a)))
from collections import deque

N = int(eval(input()))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
st = deque([[i] for i in range(N, 0, -1)])
ord = 0
a = 0
b = 0
while len(st) > 0:
    if a > 0 and b > 0:
        break
    now = st.pop()
    if len(now) == N:
        ord += 1
    if P == now:
        a = ord
    if Q == now:
        b = ord
    for i in range(N, 0, -1):
        if i in now:
            continue
        st.append(now + [i])
print((abs(b - a)))

from itertools import permutations

N = int(eval(input()))
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
perm = list(permutations(list(range(1, N + 1)), N))
i = 0
a = 0
b = 0
for i in range(len(perm)):
    if a > 0 and b > 0:
        break
    if perm[i] == P:
        a = i + 1
    if perm[i] == Q:
        b = i + 1
print((abs(b - a)))

-from collections import deque +from itertools import permutations -P = [int(x) for x in input().split()] -Q = [int(x) for x in input().split()] -st = deque([[i] for i in range(N, 0, -1)]) -ord = 0 +P = tuple(int(x) for x in input().split()) +Q = tuple(int(x) for x in input().split()) +perm = list(permutations(list(range(1, N + 1)), N)) +i = 0 -while len(st) > 0: +for i in range(len(perm)): -    now = st.pop() -    if len(now) == N: -        ord += 1 -    if P == now: -        a = ord -    if Q == now: -        b = ord -    for i in range(N, 0, -1): -        if i in now: -            continue -        st.append(now + [i]) +    if perm[i] == P: +        a = i + 1 +    if perm[i] == Q: +        b = i + 1