import itertools

n=int(eval(input()))

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

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

o=0

h=0

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

for i in range(len(u)):

    if u[i]==p:

        h=i

for i in range(len(u)):

    if u[i]==q:

        o=i

print((abs(h-o)))

import itertools

n=int(eval(input()))

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

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

o=0

h=0

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

for i in range(len(u)):

    if u[i]==p:

        h=i

    if u[i]==q:

        o=i

print((abs(h-o)))
import itertools

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

import itertools

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

-for i in range(len(u)):