

import itertools

N = int(eval(input()))

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



s = []

for i in a:

  ans = ""

  for j in i:

    ans+=str(j)

  s.append(ans)



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

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

ans = abs((s.index(P)+1)-(s.index(Q)+1))

print(ans)

# abc150_c.py

'''

全パターン作ってソートで順番みる

'''

import itertools

N = int(eval(input()))

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

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



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

target =  itertools.permutations(l)

cnt=0

pcnt = 0

qcnt = 0

for i in target:

    cnt += 1

    if i == P:

        pcnt=cnt

    if i == Q:

        qcnt=cnt



print((abs(pcnt-qcnt)))

import itertools

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

# abc150_c.py
"""
全パターン作ってソートで順番みる
"""
import itertools

N = int(eval(input()))
P = tuple(list(map(int, input().split())))
Q = tuple(list(map(int, input().split())))
l = [i + 1 for i in range(N)]
target = itertools.permutations(l)
cnt = 0
pcnt = 0
qcnt = 0
for i in target:
    cnt += 1
    if i == P:
        pcnt = cnt
    if i == Q:
        qcnt = cnt
print((abs(pcnt - qcnt)))

+# abc150_c.py +""" +全パターン作ってソートで順番みる +""" -a = list(itertools.permutations(list(range(1, N + 1)))) -s = [] -for i in a: -    ans = "" -    for j in i: -        ans += str(j) -    s.append(ans) -P = "".join(list(input().split())) -Q = "".join(list(input().split())) -ans = abs((s.index(P) + 1) - (s.index(Q) + 1)) -print(ans) +P = tuple(list(map(int, input().split()))) +Q = tuple(list(map(int, input().split()))) +l = [i + 1 for i in range(N)] +target = itertools.permutations(l) +cnt = 0 +pcnt = 0 +qcnt = 0 +for i in target: +    cnt += 1 +    if i == P: +        pcnt = cnt +    if i == Q: +        qcnt = cnt +print((abs(pcnt - qcnt)))