import itertools

N = int(eval(input()))



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

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



p = ""

q = ""

for i in P:

    p += str(i)

p = int(p)

for i in Q:

    q += str(i)

q = int(q)

tmp = itertools.permutations([i+1 for i in range(N)])

target = []

for a in tmp:

    t = ""

    for s in a:

        t += str(s)

    target.append(int(t))

qidx = 0

pidx = 0

for i, t in enumerate(target):

    if p == t:

        pidx = i

    if q == t:

        qidx = i



print((abs(pidx-qidx)))



import itertools

N = int(eval(input()))

P = ''.join(input().split())

Q = ''.join(input().split())

tmp = itertools.permutations([str(i+1) for i in range(N)])

target = []

for a in tmp:

    target.append(''.join(a))

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

import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
p = ""
q = ""
for i in P:
    p += str(i)
p = int(p)
for i in Q:
    q += str(i)
q = int(q)
tmp = itertools.permutations([i + 1 for i in range(N)])
target = []
for a in tmp:
    t = ""
    for s in a:
        t += str(s)
    target.append(int(t))
qidx = 0
pidx = 0
for i, t in enumerate(target):
    if p == t:
        pidx = i
    if q == t:
        qidx = i
print((abs(pidx - qidx)))

import itertools

N = int(eval(input()))
P = "".join(input().split())
Q = "".join(input().split())
tmp = itertools.permutations([str(i + 1) for i in range(N)])
target = []
for a in tmp:
    target.append("".join(a))
print((abs(target.index(P) - target.index(Q))))

-P = list(map(int, input().split())) -Q = list(map(int, input().split())) -p = "" -q = "" -for i in P: -    p += str(i) -p = int(p) -for i in Q: -    q += str(i) -q = int(q) -tmp = itertools.permutations([i + 1 for i in range(N)]) +P = "".join(input().split()) +Q = "".join(input().split()) +tmp = itertools.permutations([str(i + 1) for i in range(N)]) -    t = "" -    for s in a: -        t += str(s) -    target.append(int(t)) -qidx = 0 -pidx = 0 -for i, t in enumerate(target): -    if p == t: -        pidx = i -    if q == t: -        qidx = i -print((abs(pidx - qidx))) +    target.append("".join(a)) +print((abs(target.index(P) - target.index(Q))))