n = int(eval(input()))

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

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

import itertools

lst = [i+1 for i in range(n)]

l = [0]*len(lst)

for i in itertools.permutations(lst):

    l.append(list(i))

for i in range(len(l)):

    if p == l[i]:

        a = i

    if q == l[i]:

        b = i

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

n = int(eval(input()))

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

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

i = 0



for l in pm([i+1 for i in range(n)],n):

    if l == p:

        a = i

    if l == q:

        b = i

    i += 1

print((abs(a-b)))

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
import itertools

lst = [i + 1 for i in range(n)]
l = [0] * len(lst)
for i in itertools.permutations(lst):
    l.append(list(i))
for i in range(len(l)):
    if p == l[i]:
        a = i
    if q == l[i]:
        b = i
print((abs(b - a)))

from itertools import permutations as pm

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
i = 0
for l in pm([i + 1 for i in range(n)], n):
    if l == p:
        a = i
    if l == q:
        b = i
    i += 1
print((abs(a - b)))

+from itertools import permutations as pm + -p = list(map(int, input().split())) -q = list(map(int, input().split())) -import itertools - -lst = [i + 1 for i in range(n)] -l = [0] * len(lst) -for i in itertools.permutations(lst): -    l.append(list(i)) -for i in range(len(l)): -    if p == l[i]: +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +i = 0 +for l in pm([i + 1 for i in range(n)], n): +    if l == p: -    if q == l[i]: +    if l == q: -print((abs(b - a))) +    i += 1 +print((abs(a - b)))