import itertools

N = int(eval(input()))

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

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

A = [i for i in range(1,N+1)]

count = 1

for i in itertools.permutations(A):

    flg = True

    for k in range(N):

        if i[k] != P[k]:

            flg = False

            break

    if flg:

        a = count

    flg = True

    for k in range(N):

        if i[k] != Q[k]:

            flg = False

            break

    if flg:

        b = count

    count += 1

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

N = int(eval(input()))

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

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

a = [i for i in range(1,N+1)]

cnt = 0

ans_a, ans_b = 0, 0

for target in itertools.permutations(a):

    cnt += 1

    if list(target) == P:

        ans_a = cnt

    if list(target) == Q:

        ans_b = cnt

print((abs(ans_a-ans_b)))
import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
A = [i for i in range(1, N + 1)]
count = 1
for i in itertools.permutations(A):
    flg = True
    for k in range(N):
        if i[k] != P[k]:
            flg = False
            break
    if flg:
        a = count
    flg = True
    for k in range(N):
        if i[k] != Q[k]:
            flg = False
            break
    if flg:
        b = count
    count += 1
print((abs(a - b)))

import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = [i for i in range(1, N + 1)]
cnt = 0
ans_a, ans_b = 0, 0
for target in itertools.permutations(a):
    cnt += 1
    if list(target) == P:
        ans_a = cnt
    if list(target) == Q:
        ans_b = cnt
print((abs(ans_a - ans_b)))

-A = [i for i in range(1, N + 1)] -count = 1 -for i in itertools.permutations(A): -    flg = True -    for k in range(N): -        if i[k] != P[k]: -            flg = False -            break -    if flg: -        a = count -    flg = True -    for k in range(N): -        if i[k] != Q[k]: -            flg = False -            break -    if flg: -        b = count -    count += 1 -print((abs(a - b))) +a = [i for i in range(1, N + 1)] +cnt = 0 +ans_a, ans_b = 0, 0 +for target in itertools.permutations(a): +    cnt += 1 +    if list(target) == P: +        ans_a = cnt +    if list(target) == Q: +        ans_b = cnt +print((abs(ans_a - ans_b)))