import itertools



N = int(eval(input()))

P = int(input().replace(' ', ''))

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

s = [i for i in range(1, N + 1)]

l = list(itertools.permutations(s))

j = []

for i in range(len(l)):

  t = list(map(str,l[i]))

  j.append(int(''.join(t)))



nl = sorted(j) 

a = 0

b = 0



for i in range(len(nl)):

  if nl[i] == P:

    a = i + 1

  if nl[i] == Q:

    b = i + 1

    

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



N = int(eval(input()))

P = int(input().replace(' ', ''))

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

l = [i for i in range(1, N+1)]

pl = itertools.permutations(l)

lis = []

for oc in pl:

  lis.append(int(''.join(map(str, list(oc)))))

lis.sort()

a = lis.index(P)

b = lis.index(Q)



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

N = int(eval(input()))
P = int(input().replace(" ", ""))
Q = int(input().replace(" ", ""))
s = [i for i in range(1, N + 1)]
l = list(itertools.permutations(s))
j = []
for i in range(len(l)):
    t = list(map(str, l[i]))
    j.append(int("".join(t)))
nl = sorted(j)
a = 0
b = 0
for i in range(len(nl)):
    if nl[i] == P:
        a = i + 1
    if nl[i] == Q:
        b = i + 1
print((abs(a - b)))

import itertools

N = int(eval(input()))
P = int(input().replace(" ", ""))
Q = int(input().replace(" ", ""))
l = [i for i in range(1, N + 1)]
pl = itertools.permutations(l)
lis = []
for oc in pl:
    lis.append(int("".join(map(str, list(oc)))))
lis.sort()
a = lis.index(P)
b = lis.index(Q)
print((abs(a - b)))

-s = [i for i in range(1, N + 1)] -l = list(itertools.permutations(s)) -j = [] -for i in range(len(l)): -    t = list(map(str, l[i])) -    j.append(int("".join(t))) -nl = sorted(j) -a = 0 -b = 0 -for i in range(len(nl)): -    if nl[i] == P: -        a = i + 1 -    if nl[i] == Q: -        b = i + 1 +l = [i for i in range(1, N + 1)] +pl = itertools.permutations(l) +lis = [] +for oc in pl: +    lis.append(int("".join(map(str, list(oc))))) +lis.sort() +a = lis.index(P) +b = lis.index(Q)