import itertools



n = int(eval(input()))

P = input().split()

numbers = list(itertools.permutations(P))

P = ''.join(P)

Q = eval(input())

Q = Q.replace(' ', '')



numbers_str = []

for num in numbers:

    numbers_str.append(''.join(num))

numbers_str.sort()

print((abs(numbers_str.index(P)-numbers_str.index(Q))))

import itertools



n = int(eval(input()))

P = input().split()

Q = input().split()

p = ''.join(P)

q = ''.join(Q)

P.sort()

numbers = list(itertools.permutations(P))



numbers_str = []

for num in numbers:

    numbers_str.append(''.join(num))



print((abs(numbers_str.index(p)-numbers_str.index(q))))

import itertools

n = int(eval(input()))
P = input().split()
numbers = list(itertools.permutations(P))
P = "".join(P)
Q = eval(input())
Q = Q.replace(" ", "")
numbers_str = []
for num in numbers:
    numbers_str.append("".join(num))
numbers_str.sort()
print((abs(numbers_str.index(P) - numbers_str.index(Q))))

import itertools

n = int(eval(input()))
P = input().split()
Q = input().split()
p = "".join(P)
q = "".join(Q)
P.sort()
numbers = list(itertools.permutations(P))
numbers_str = []
for num in numbers:
    numbers_str.append("".join(num))
print((abs(numbers_str.index(p) - numbers_str.index(q))))

+Q = input().split() +p = "".join(P) +q = "".join(Q) +P.sort() -P = "".join(P) -Q = eval(input()) -Q = Q.replace(" ", "") -numbers_str.sort() -print((abs(numbers_str.index(P) - numbers_str.index(Q)))) +print((abs(numbers_str.index(p) - numbers_str.index(q))))