n=int(eval(input()))

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

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

aa=()

import itertools

c=list(itertools.permutations(list(range(1,n+1))))

cc=[]

for i in c:

    cc.append(list(i))

for x,i in enumerate(cc):

    if a==i:

        aa=x

    if b==i:

        bb=x

print((abs(aa-bb)))
n=int(eval(input()))

a=tuple([int(x)-1 for x in input().split()])

b=tuple([int(x)-1 for x in input().split()])



import itertools as it

pe=list(it.permutations(list(range(n)),n))



x,y=0,0

for ind,i in enumerate(pe):

    if i==a:

        x=ind

    if i==b:

        y=ind

print((abs(x-y)))
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = ()
import itertools

c = list(itertools.permutations(list(range(1, n + 1))))
cc = []
for i in c:
    cc.append(list(i))
for x, i in enumerate(cc):
    if a == i:
        aa = x
    if b == i:
        bb = x
print((abs(aa - bb)))

n = int(eval(input()))
a = tuple([int(x) - 1 for x in input().split()])
b = tuple([int(x) - 1 for x in input().split()])
import itertools as it

pe = list(it.permutations(list(range(n)), n))
x, y = 0, 0
for ind, i in enumerate(pe):
    if i == a:
        x = ind
    if i == b:
        y = ind
print((abs(x - y)))

-a = list(map(int, input().split())) -b = list(map(int, input().split())) -aa = () -import itertools +a = tuple([int(x) - 1 for x in input().split()]) +b = tuple([int(x) - 1 for x in input().split()]) +import itertools as it -c = list(itertools.permutations(list(range(1, n + 1)))) -cc = [] -for i in c: -    cc.append(list(i)) -for x, i in enumerate(cc): -    if a == i: -        aa = x -    if b == i: -        bb = x -print((abs(aa - bb))) +pe = list(it.permutations(list(range(n)), n)) +x, y = 0, 0 +for ind, i in enumerate(pe): +    if i == a: +        x = ind +    if i == b: +        y = ind +print((abs(x - y)))