N=int(eval(input()))

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

ps=""

for i in range(N):

  ps+=str(P[i])

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

qs=""

for i in range(N):

  qs+=str(Q[i])



stack=[]

stack.append("")

res=[]

while stack:

  d=stack.pop()

  if len(d)==N:

    res.append(d)

    continue

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

    if str(i) not in d:

      stack.append(d+str(i))

  

res=res[::-1]

a=0

b=0

for i in range(len(res)):

  if res[i]==ps:

    a=i+1

  if res[i]==qs:

    b=i+1



print((abs(a-b)))

import sys

readline = sys.stdin.readline



N = int(readline())

P = tuple(map(int,readline().split()))

Q = tuple(map(int,readline().split()))



import itertools

a = 0

b = 0

cnt = 0

for perm in itertools.permutations(list(range(1, N + 1))):

  cnt += 1

  if perm == P:

    a = cnt

  if perm == Q:

    b = cnt

    

print((abs(a - b)))    
N = int(eval(input()))
P = list(map(int, input().split()))
ps = ""
for i in range(N):
    ps += str(P[i])
Q = list(map(int, input().split()))
qs = ""
for i in range(N):
    qs += str(Q[i])
stack = []
stack.append("")
res = []
while stack:
    d = stack.pop()
    if len(d) == N:
        res.append(d)
        continue
    for i in range(1, N + 1):
        if str(i) not in d:
            stack.append(d + str(i))
res = res[::-1]
a = 0
b = 0
for i in range(len(res)):
    if res[i] == ps:
        a = i + 1
    if res[i] == qs:
        b = i + 1
print((abs(a - b)))

import sys

readline = sys.stdin.readline
N = int(readline())
P = tuple(map(int, readline().split()))
Q = tuple(map(int, readline().split()))
import itertools

a = 0
b = 0
cnt = 0
for perm in itertools.permutations(list(range(1, N + 1))):
    cnt += 1
    if perm == P:
        a = cnt
    if perm == Q:
        b = cnt
print((abs(a - b)))

-N = int(eval(input())) -P = list(map(int, input().split())) -ps = "" -for i in range(N): -    ps += str(P[i]) -Q = list(map(int, input().split())) -qs = "" -for i in range(N): -    qs += str(Q[i]) -stack = [] -stack.append("") -res = [] -while stack: -    d = stack.pop() -    if len(d) == N: -        res.append(d) -        continue -    for i in range(1, N + 1): -        if str(i) not in d: -            stack.append(d + str(i)) -res = res[::-1] +import sys + +readline = sys.stdin.readline +N = int(readline()) +P = tuple(map(int, readline().split())) +Q = tuple(map(int, readline().split())) +import itertools + -for i in range(len(res)): -    if res[i] == ps: -        a = i + 1 -    if res[i] == qs: -        b = i + 1 +cnt = 0 +for perm in itertools.permutations(list(range(1, N + 1))): +    cnt += 1 +    if perm == P: +        a = cnt +    if perm == Q: +        b = cnt