#abc150c

from itertools import *



n=int(eval(input()))

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

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

l=list(permutations(list(range(1,n+1))))

print((abs(l.index(p)-l.index(q))))

#abc150c

from itertools import *



n=int(input())

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

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

l=list(permutations(list(range(1,n+1))))

print(abs(l.index(p)-l.index(q)))

# abc150c
from itertools import *

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = list(permutations(list(range(1, n + 1))))
print((abs(l.index(p) - l.index(q))))

# abc150c
from itertools import *

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = list(permutations(list(range(1, n + 1))))
print(abs(l.index(p) - l.index(q)))

-n = int(eval(input())) +n = int(input()) -print((abs(l.index(p) - l.index(q)))) +print(abs(l.index(p) - l.index(q)))