import itertools



N = int(eval(input()))

P = [int(x) for x in input().split()]

P = tuple(P)

Q = [int(x) for x in input().split()]

Q = tuple(Q)

N_ = list(range(1, N+1))



R = list(itertools.permutations(N_))



Pflg = Qflg = False

Pcnt = Qcnt = -1

for i, r in enumerate(R):

    if P==r:

        Pflg = True

        Pcnt = i

    if Q==r:

        Qflg = True

        Qcnt = i

    if Pflg and Qflg:

        break



print((abs(Pcnt - Qcnt)))
import itertools



N = int(eval(input()))

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

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



pmt = list(itertools.permutations(list(range(1, N+1))))

pmtP = pmt.index(P)

pmtQ = pmt.index(Q)

print((abs(pmtP - pmtQ)))
import itertools

N = int(eval(input()))
P = [int(x) for x in input().split()]
P = tuple(P)
Q = [int(x) for x in input().split()]
Q = tuple(Q)
N_ = list(range(1, N + 1))
R = list(itertools.permutations(N_))
Pflg = Qflg = False
Pcnt = Qcnt = -1
for i, r in enumerate(R):
    if P == r:
        Pflg = True
        Pcnt = i
    if Q == r:
        Qflg = True
        Qcnt = i
    if Pflg and Qflg:
        break
print((abs(Pcnt - Qcnt)))

import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
pmt = list(itertools.permutations(list(range(1, N + 1))))
pmtP = pmt.index(P)
pmtQ = pmt.index(Q)
print((abs(pmtP - pmtQ)))

-P = [int(x) for x in input().split()] -P = tuple(P) -Q = [int(x) for x in input().split()] -Q = tuple(Q) -N_ = list(range(1, N + 1)) -R = list(itertools.permutations(N_)) -Pflg = Qflg = False -Pcnt = Qcnt = -1 -for i, r in enumerate(R): -    if P == r: -        Pflg = True -        Pcnt = i -    if Q == r: -        Qflg = True -        Qcnt = i -    if Pflg and Qflg: -        break -print((abs(Pcnt - Qcnt))) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +pmt = list(itertools.permutations(list(range(1, N + 1)))) +pmtP = pmt.index(P) +pmtQ = pmt.index(Q) +print((abs(pmtP - pmtQ)))