import sys



n = int(eval(input()))

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

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





last = [n-1 for i in range(n)]



a,b = 0,0



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

if l == p:

	a = 1

if l == q:

	b = 1



if a == b == 1:

	print((0))

	sys.exit()

cnt = 1

while True:

	cnt += 1

	for i in range(n-1,-1,-1):

		if l[i] < l[i-1]:

			continue

		else:

			obj = l[i-1]

			tmp = sorted(l[i-1:])

			switch = tmp.pop(tmp.index(obj)+1)

			l = l[:i-1] + [switch] + sorted(tmp)



			#print(l)



			break

	

	if l == p:

		a = cnt

	if l == q:

		b = cnt



	if a != 0 and b != 0:

		break



print((abs(a-b)))



import itertools



n = int(eval(input()))

p = "".join(list(map(str, input().split())))

q = "".join(list(map(str, input().split())))



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



cnt = 0

a,b = 0,0

for x in itertools.permutations(l):

	y = "".join(x)

	cnt += 1

	if y == p:

		a = cnt

	if y == q:

		b = cnt



	if a != 0 and b != 0:

		break



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

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
last = [n - 1 for i in range(n)]
a, b = 0, 0
l = [i + 1 for i in range(n)]
if l == p:
    a = 1
if l == q:
    b = 1
if a == b == 1:
    print((0))
    sys.exit()
cnt = 1
while True:
    cnt += 1
    for i in range(n - 1, -1, -1):
        if l[i] < l[i - 1]:
            continue
        else:
            obj = l[i - 1]
            tmp = sorted(l[i - 1 :])
            switch = tmp.pop(tmp.index(obj) + 1)
            l = l[: i - 1] + [switch] + sorted(tmp)
            # print(l)
            break
    if l == p:
        a = cnt
    if l == q:
        b = cnt
    if a != 0 and b != 0:
        break
print((abs(a - b)))

import itertools

n = int(eval(input()))
p = "".join(list(map(str, input().split())))
q = "".join(list(map(str, input().split())))
l = "".join([str(i + 1) for i in range(n)])
cnt = 0
a, b = 0, 0
for x in itertools.permutations(l):
    y = "".join(x)
    cnt += 1
    if y == p:
        a = cnt
    if y == q:
        b = cnt
    if a != 0 and b != 0:
        break
print((abs(a - b)))

-import sys +import itertools -p = list(map(int, input().split())) -q = list(map(int, input().split())) -last = [n - 1 for i in range(n)] +p = "".join(list(map(str, input().split()))) +q = "".join(list(map(str, input().split()))) +l = "".join([str(i + 1) for i in range(n)]) +cnt = 0 -l = [i + 1 for i in range(n)] -if l == p: -    a = 1 -if l == q: -    b = 1 -if a == b == 1: -    print((0)) -    sys.exit() -cnt = 1 -while True: +for x in itertools.permutations(l): +    y = "".join(x) -    for i in range(n - 1, -1, -1): -        if l[i] < l[i - 1]: -            continue -        else: -            obj = l[i - 1] -            tmp = sorted(l[i - 1 :]) -            switch = tmp.pop(tmp.index(obj) + 1) -            l = l[: i - 1] + [switch] + sorted(tmp) -            # print(l) -            break -    if l == p: +    if y == p: -    if l == q: +    if y == q: