import itertools

 

n = int(eval(input()))

p = list(input().split())

q = list(input().split())



def dic(z):

    # sorted(list(itertools.permutations(z))) の中に辞書順に並べられた順列が入っている

    return sorted(list(itertools.permutations(z))).index(tuple(z)) + 1

 

print((abs(dic(p) - dic(q))))
n = int(eval(input()))

p = eval(input())

q = eval(input())



import itertools



l = []

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

    l.append(str(i))



ans = []

for v in itertools.permutations(l):

    ans.append(" ".join(v))



a = ans.index(p)

b = ans.index(q)

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

n = int(eval(input()))
p = list(input().split())
q = list(input().split())


def dic(z):
    # sorted(list(itertools.permutations(z))) の中に辞書順に並べられた順列が入っている
    return sorted(list(itertools.permutations(z))).index(tuple(z)) + 1


print((abs(dic(p) - dic(q))))

n = int(eval(input()))
p = eval(input())
q = eval(input())
import itertools

l = []
for i in range(1, n + 1):
    l.append(str(i))
ans = []
for v in itertools.permutations(l):
    ans.append(" ".join(v))
a = ans.index(p)
b = ans.index(q)
print((abs(a - b)))

+n = int(eval(input())) +p = eval(input()) +q = eval(input()) -n = int(eval(input())) -p = list(input().split()) -q = list(input().split()) - - -def dic(z): -    # sorted(list(itertools.permutations(z))) の中に辞書順に並べられた順列が入っている -    return sorted(list(itertools.permutations(z))).index(tuple(z)) + 1 - - -print((abs(dic(p) - dic(q)))) +l = [] +for i in range(1, n + 1): +    l.append(str(i)) +ans = [] +for v in itertools.permutations(l): +    ans.append(" ".join(v)) +a = ans.index(p) +b = ans.index(q) +print((abs(a - b)))