N=int(eval(input()))

P=list(input().split())

Q=list(input().split())

P=int(''.join(P))

Q=int(''.join(Q))

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

A=[]

def dps(n,A,me):

    if len(n)==0:

        A.append(me)

        return

    else:

        for i in range(0, len(n)):

            t=n.pop(i)

            dps(n, A, me*10+t)

            n.insert(i, t)

dps(n,A,0)

print((abs(A.index(P)-A.index(Q))))
N=int(eval(input()))

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

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

import itertools

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

print((abs(l.index(P)-l.index(Q))))

N = int(eval(input()))
P = list(input().split())
Q = list(input().split())
P = int("".join(P))
Q = int("".join(Q))
n = [i for i in range(1, N + 1)]
A = []


def dps(n, A, me):
    if len(n) == 0:
        A.append(me)
        return
    else:
        for i in range(0, len(n)):
            t = n.pop(i)
            dps(n, A, me * 10 + t)
            n.insert(i, t)


dps(n, A, 0)
print((abs(A.index(P) - A.index(Q))))

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
import itertools

l = list(itertools.permutations([i for i in range(1, N + 1)]))
print((abs(l.index(P) - l.index(Q))))

-P = list(input().split()) -Q = list(input().split()) -P = int("".join(P)) -Q = int("".join(Q)) -n = [i for i in range(1, N + 1)] -A = [] +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +import itertools - -def dps(n, A, me): -    if len(n) == 0: -        A.append(me) -        return -    else: -        for i in range(0, len(n)): -            t = n.pop(i) -            dps(n, A, me * 10 + t) -            n.insert(i, t) - - -dps(n, A, 0) -print((abs(A.index(P) - A.index(Q)))) +l = list(itertools.permutations([i for i in range(1, N + 1)])) +print((abs(l.index(P) - l.index(Q))))