from itertools import permutations

stdin = open(0).read().split('\n')

N = int(stdin[0])



P=int("".join(list(stdin[1].split())))

Q=int("".join(list(stdin[2].split())))



l = []

for pp in permutations(list(range(1, N+1))):

    ll = []

    for _p in pp:

        ll.append(str(_p))

    l.append(int("".join(ll)))

a = l.index(P)

b = l.index(Q)

print((abs(a - b)))
def perm(N):

    if N==0:return 0

    a=1

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

        a*=i

    return a



def getposi(P):

    N=len(P)

    l=list(range(1,N+1))

    a = 0

    for i,num in enumerate(P):

        a+=perm(N-i-1)*l.index(num)

        l.remove(num)

    return a



stdin = open(0).read().split('\n')

N = int(stdin[0])

P = list(map(int,stdin[1].split()))

Q = list(map(int,stdin[2].split()))



a=getposi(P)

b=getposi(Q)



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

stdin = open(0).read().split("\n")
N = int(stdin[0])
P = int("".join(list(stdin[1].split())))
Q = int("".join(list(stdin[2].split())))
l = []
for pp in permutations(list(range(1, N + 1))):
    ll = []
    for _p in pp:
        ll.append(str(_p))
    l.append(int("".join(ll)))
a = l.index(P)
b = l.index(Q)
print((abs(a - b)))

def perm(N):
    if N == 0:
        return 0
    a = 1
    for i in range(1, N + 1):
        a *= i
    return a


def getposi(P):
    N = len(P)
    l = list(range(1, N + 1))
    a = 0
    for i, num in enumerate(P):
        a += perm(N - i - 1) * l.index(num)
        l.remove(num)
    return a


stdin = open(0).read().split("\n")
N = int(stdin[0])
P = list(map(int, stdin[1].split()))
Q = list(map(int, stdin[2].split()))
a = getposi(P)
b = getposi(Q)
print((abs(a - b)))

-from itertools import permutations +def perm(N): +    if N == 0: +        return 0 +    a = 1 +    for i in range(1, N + 1): +        a *= i +    return a + + +def getposi(P): +    N = len(P) +    l = list(range(1, N + 1)) +    a = 0 +    for i, num in enumerate(P): +        a += perm(N - i - 1) * l.index(num) +        l.remove(num) +    return a + -P = int("".join(list(stdin[1].split()))) -Q = int("".join(list(stdin[2].split()))) -l = [] -for pp in permutations(list(range(1, N + 1))): -    ll = [] -    for _p in pp: -        ll.append(str(_p)) -    l.append(int("".join(ll))) -a = l.index(P) -b = l.index(Q) +P = list(map(int, stdin[1].split())) +Q = list(map(int, stdin[2].split())) +a = getposi(P) +b = getposi(Q)