N = int(eval(input()))

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

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

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



import itertools

L = list(itertools.permutations(l))



jisyo = []

for i in range(len(L)):

  n = 0

  for j in range(N):

    n = n * 10 + L[i][j]

  jisyo.append(n)



p = 0

q = 0

for x in range(N):

  p = p * 10 + P[x]

  q = q * 10 + Q[x]



print((abs(jisyo.index(p) - jisyo.index(q))))
import itertools

n = int(eval(input()))

p = tuple(map(int, input().split()))

q = tuple(map(int, input().split()))

cnt = 0

for v in itertools.permutations(list(range(1,n+1))):

    cnt += 1

    if p == v: a = cnt

    if q == v: b = cnt

print((abs(a-b)))
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
l = list(range(1, N + 1))
import itertools

L = list(itertools.permutations(l))
jisyo = []
for i in range(len(L)):
    n = 0
    for j in range(N):
        n = n * 10 + L[i][j]
    jisyo.append(n)
p = 0
q = 0
for x in range(N):
    p = p * 10 + P[x]
    q = q * 10 + Q[x]
print((abs(jisyo.index(p) - jisyo.index(q))))

import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
cnt = 0
for v in itertools.permutations(list(range(1, n + 1))):
    cnt += 1
    if p == v:
        a = cnt
    if q == v:
        b = cnt
print((abs(a - b)))

-N = int(eval(input())) -P = list(map(int, input().split())) -Q = list(map(int, input().split())) -l = list(range(1, N + 1)) -L = list(itertools.permutations(l)) -jisyo = [] -for i in range(len(L)): -    n = 0 -    for j in range(N): -        n = n * 10 + L[i][j] -    jisyo.append(n) -p = 0 -q = 0 -for x in range(N): -    p = p * 10 + P[x] -    q = q * 10 + Q[x] -print((abs(jisyo.index(p) - jisyo.index(q)))) +n = int(eval(input())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +cnt = 0 +for v in itertools.permutations(list(range(1, n + 1))): +    cnt += 1 +    if p == v: +        a = cnt +    if q == v: +        b = cnt +print((abs(a - b)))