import itertools

n = int(eval(input()))

ls = []

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

    ls.append(i)

    

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

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



a = 0

b = 0

c = 1

for v in itertools.permutations(ls, n):

    if list(v) == p:

        a = c

    if list(v) == q:

        b = c

    c += 1

    

print((abs(a-b)))
#ABC150C

import itertools

n = int(eval(input()))

ls = [i+1 for i in range(n)]

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

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

v = [ list(v) for v in itertools.permutations(ls, n)]

a, b = v.index(p), v.index(q)

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

n = int(eval(input()))
ls = []
for i in range(1, n + 1):
    ls.append(i)
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a = 0
b = 0
c = 1
for v in itertools.permutations(ls, n):
    if list(v) == p:
        a = c
    if list(v) == q:
        b = c
    c += 1
print((abs(a - b)))

# ABC150C
import itertools

n = int(eval(input()))
ls = [i + 1 for i in range(n)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
v = [list(v) for v in itertools.permutations(ls, n)]
a, b = v.index(p), v.index(q)
print((abs(a - b)))

+# ABC150C -ls = [] -for i in range(1, n + 1): -    ls.append(i) +ls = [i + 1 for i in range(n)] -a = 0 -b = 0 -c = 1 -for v in itertools.permutations(ls, n): -    if list(v) == p: -        a = c -    if list(v) == q: -        b = c -    c += 1 +v = [list(v) for v in itertools.permutations(ls, n)] +a, b = v.index(p), v.index(q)