import itertools

n = int(eval(input()))

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

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

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

r = list(itertools.permutations(l))

j = 0

flagp = True

flagq = True

while flagp:

  if r[j] == p:

    x = j+1

    flagp = False

  j += 1

j = 0

while flagq:

  if r[j] == q:

    y = j+1

    flagq = False

  j += 1

print((abs(y-x)))
from itertools import permutations

n = int(eval(input()))

p = tuple(input().split())

q = tuple(input().split())



# print(p)

# print(q)



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



cnt = 1

for v in permutations(l,n):

    if p == v:

        a = cnt

    if q == v:

        b = cnt

    cnt += 1

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

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = [i + 1 for i in range(n)]
r = list(itertools.permutations(l))
j = 0
flagp = True
flagq = True
while flagp:
    if r[j] == p:
        x = j + 1
        flagp = False
    j += 1
j = 0
while flagq:
    if r[j] == q:
        y = j + 1
        flagq = False
    j += 1
print((abs(y - x)))

from itertools import permutations

n = int(eval(input()))
p = tuple(input().split())
q = tuple(input().split())
# print(p)
# print(q)
l = [str(i + 1) for i in range(n)]
cnt = 1
for v in permutations(l, n):
    if p == v:
        a = cnt
    if q == v:
        b = cnt
    cnt += 1
print((abs(a - b)))

-import itertools +from itertools import permutations -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -l = [i + 1 for i in range(n)] -r = list(itertools.permutations(l)) -j = 0 -flagp = True -flagq = True -while flagp: -    if r[j] == p: -        x = j + 1 -        flagp = False -    j += 1 -j = 0 -while flagq: -    if r[j] == q: -        y = j + 1 -        flagq = False -    j += 1 -print((abs(y - x))) +p = tuple(input().split()) +q = tuple(input().split()) +# print(p) +# print(q) +l = [str(i + 1) for i in range(n)] +cnt = 1 +for v in permutations(l, n): +    if p == v: +        a = cnt +    if q == v: +        b = cnt +    cnt += 1 +print((abs(a - b)))