import itertools

n = int(eval(input()))

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

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

po = 0

qo = 0

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

for i in range(len(l)):

    if l[i] == p:

        po = i

    if l[i]== q:

        qo = i

print((abs(po-qo)))
import itertools

n = int(eval(input()))

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

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

l = sorted(itertools.permutations([i for i in range(1,n+1)]))

print((abs(l.index(tuple(p))-l.index(tuple(q)))))
import itertools

n = int(eval(input()))
p = tuple(list(map(int, input().split())))
q = tuple(list(map(int, input().split())))
po = 0
qo = 0
l = sorted(list(itertools.permutations(list(range(1, n + 1)))))
for i in range(len(l)):
    if l[i] == p:
        po = i
    if l[i] == q:
        qo = i
print((abs(po - qo)))

import itertools

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

-p = tuple(list(map(int, input().split()))) -q = tuple(list(map(int, input().split()))) -po = 0 -qo = 0 -l = sorted(list(itertools.permutations(list(range(1, n + 1))))) -for i in range(len(l)): -    if l[i] == p: -        po = i -    if l[i] == q: -        qo = i -print((abs(po - qo))) +p = list(map(int, input().split())) +q = list(map(int, input().split())) +l = sorted(itertools.permutations([i for i in range(1, n + 1)])) +print((abs(l.index(tuple(p)) - l.index(tuple(q)))))