import itertools





n = int(eval(input()))

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

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



junretsu = []

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

    junretsu.append(i)

junretsu = sorted(junretsu)

a = junretsu.index(p)

b = junretsu.index(q)



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





n = int(eval(input()))

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

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



junretsu = itertools.permutations(list(range(1, n + 1)))

junretsu = sorted(junretsu)

a = junretsu.index(p)

b = junretsu.index(q)



print((abs(a - b)))

import itertools

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

import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
junretsu = itertools.permutations(list(range(1, n + 1)))
junretsu = sorted(junretsu)
a = junretsu.index(p)
b = junretsu.index(q)
print((abs(a - b)))

-junretsu = [] -for i in itertools.permutations(list(range(1, n + 1))): -    junretsu.append(i) +junretsu = itertools.permutations(list(range(1, n + 1)))