from itertools import permutations 



n = int(eval(input()))

p = [int(i) for i in input().split()]

q = [int(i) for i in input().split()]

a = 0

b = 0

cnt = 0

st = 0

for i in permutations(list(range(1,n+1))):

    cnt += 1

    if list(i) == p:

        a = cnt

        st += 1

    if list(i) == q:

        b = cnt

        st += 1

    if st == 2:

        break

    

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



n = int(eval(input()))

p = tuple(int(i) for i in input().split())

q = tuple(int(i) for i in input().split())

a = 0

b = 0

cnt = 0

st = 0

for i in permutations(list(range(1,n+1))):

    cnt += 1

    if i == p:

        a = cnt

        st += 1

    if i == q:

        b = cnt

        st += 1

    if st == 2:

        break

    

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

n = int(eval(input()))
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
a = 0
b = 0
cnt = 0
st = 0
for i in permutations(list(range(1, n + 1))):
    cnt += 1
    if list(i) == p:
        a = cnt
        st += 1
    if list(i) == q:
        b = cnt
        st += 1
    if st == 2:
        break
print((abs(a - b)))

from itertools import permutations

n = int(eval(input()))
p = tuple(int(i) for i in input().split())
q = tuple(int(i) for i in input().split())
a = 0
b = 0
cnt = 0
st = 0
for i in permutations(list(range(1, n + 1))):
    cnt += 1
    if i == p:
        a = cnt
        st += 1
    if i == q:
        b = cnt
        st += 1
    if st == 2:
        break
print((abs(a - b)))

-p = [int(i) for i in input().split()] -q = [int(i) for i in input().split()] +p = tuple(int(i) for i in input().split()) +q = tuple(int(i) for i in input().split()) -    if list(i) == p: +    if i == p: -    if list(i) == q: +    if i == q: