from itertools import permutations



n=int(eval(input()))

P=list(map(str,input().split()))

Q=list(map(str,input().split()))



S=sorted(P)



S=list(permutations(S))

A=[]

for i in S:

    A.append(int("".join(i)))



A.sort()



for i in range(len(A)):

    if A[i]==int("".join(P)):

        pp=i

    if A[i]==int("".join(Q)):

        qq=i

print((abs(pp-qq)))
from itertools import permutations

n=int(eval(input()))

P=list(input().split())

Q=list(input().split())

A=[]

for i in list(permutations(P)):

   A.append(int("".join(i)))

A.sort()

print((abs(A.index(int("".join(P)))-A.index(int("".join(Q))))))
from itertools import permutations

n = int(eval(input()))
P = list(map(str, input().split()))
Q = list(map(str, input().split()))
S = sorted(P)
S = list(permutations(S))
A = []
for i in S:
    A.append(int("".join(i)))
A.sort()
for i in range(len(A)):
    if A[i] == int("".join(P)):
        pp = i
    if A[i] == int("".join(Q)):
        qq = i
print((abs(pp - qq)))

from itertools import permutations

n = int(eval(input()))
P = list(input().split())
Q = list(input().split())
A = []
for i in list(permutations(P)):
    A.append(int("".join(i)))
A.sort()
print((abs(A.index(int("".join(P))) - A.index(int("".join(Q))))))

-P = list(map(str, input().split())) -Q = list(map(str, input().split())) -S = sorted(P) -S = list(permutations(S)) +P = list(input().split()) +Q = list(input().split()) -for i in S: +for i in list(permutations(P)): -for i in range(len(A)): -    if A[i] == int("".join(P)): -        pp = i -    if A[i] == int("".join(Q)): -        qq = i -print((abs(pp - qq))) +print((abs(A.index(int("".join(P))) - A.index(int("".join(Q))))))