import itertools

N = int(eval(input()))

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

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



lis = [i for i in range(1, N+1)]

a = 0

b = 0

ans = list(itertools.permutations(lis))

TP=tuple(P)

TQ=tuple(Q)

for n, i in enumerate(ans):

    if(i == TP):

        a=n+1

        if(b!=0):

            break

    if(i==TQ):

        b=n+1

        if(a!=0):

            break



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

N=int(eval(input()))

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

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



l=[i for i in range(1,N+1)]

for n,t in enumerate(itertools.permutations(l,N)):

    if(t==P):

        a=n

    if(t==Q):

        b=n



print((abs(a-b)))





import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
lis = [i for i in range(1, N + 1)]
a = 0
b = 0
ans = list(itertools.permutations(lis))
TP = tuple(P)
TQ = tuple(Q)
for n, i in enumerate(ans):
    if i == TP:
        a = n + 1
        if b != 0:
            break
    if i == TQ:
        b = n + 1
        if a != 0:
            break
print((abs(a - b)))

import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = [i for i in range(1, N + 1)]
for n, t in enumerate(itertools.permutations(l, N)):
    if t == P:
        a = n
    if t == Q:
        b = n
print((abs(a - b)))

-P = list(map(int, input().split())) -Q = list(map(int, input().split())) -lis = [i for i in range(1, N + 1)] -a = 0 -b = 0 -ans = list(itertools.permutations(lis)) -TP = tuple(P) -TQ = tuple(Q) -for n, i in enumerate(ans): -    if i == TP: -        a = n + 1 -        if b != 0: -            break -    if i == TQ: -        b = n + 1 -        if a != 0: -            break +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +l = [i for i in range(1, N + 1)] +for n, t in enumerate(itertools.permutations(l, N)): +    if t == P: +        a = n +    if t == Q: +        b = n