import itertools

s = 1

a = 0

b = 0

N = int(eval(input()))

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

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

ite = [x for x in range(1, N + 1)]

ite = itertools.permutations(ite)

for i in ite:

    if list(i) == p:

        a = s

    s += 1

s = 1

ite = [x for x in range(1, N + 1)]

ite = itertools.permutations(ite)

for i in ite:

    if list(i) == q:

        b = s

    s += 1

if a > b:

    total = a - b

else:

    total = b - a

print(total)
import itertools

N = int(eval(input()))

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

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

ite = [x for x in range(1, N + 1)]

ite = itertools.permutations(ite)

a = 0

b = 0

s = 1

for i in ite:

    if i == p:

        a = s

    if i == q:

        b = s

    s += 1

if a > b:

    total = a - b

else:

    total = b - a

print(total)
import itertools

s = 1
a = 0
b = 0
N = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == p:
        a = s
    s += 1
s = 1
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

import itertools

N = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
a = 0
b = 0
s = 1
for i in ite:
    if i == p:
        a = s
    if i == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

-s = 1 +N = int(eval(input())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +ite = [x for x in range(1, N + 1)] +ite = itertools.permutations(ite) -N = int(eval(input())) -p = list(map(int, input().split())) -q = list(map(int, input().split())) -ite = [x for x in range(1, N + 1)] -ite = itertools.permutations(ite) +s = 1 -    if list(i) == p: +    if i == p: -    s += 1 -s = 1 -ite = [x for x in range(1, N + 1)] -ite = itertools.permutations(ite) -for i in ite: -    if list(i) == q: +    if i == q: