from numpy import prod



n = int(eval(input()))



def solve(l:list):

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

  res = 0

  for i in range(n):

    res += now.index(l[i]) * prod([j+1 for j in range(len(now)-1)])

    now.remove(l[i])

  return res



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

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



print((int(abs(solve(l_p) - solve(l_q)))))

from itertools import permutations



n = int(eval(input()))

all_ = list(permutations([i+1 for i in range(n)], n))

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

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



res = abs(all_.index(l_p) - all_.index(l_q))

print(res)
from numpy import prod

n = int(eval(input()))


def solve(l: list):
    now = [i + 1 for i in range(n)]
    res = 0
    for i in range(n):
        res += now.index(l[i]) * prod([j + 1 for j in range(len(now) - 1)])
        now.remove(l[i])
    return res


l_p = list(map(int, input().split()))
l_q = list(map(int, input().split()))
print((int(abs(solve(l_p) - solve(l_q)))))

from itertools import permutations

n = int(eval(input()))
all_ = list(permutations([i + 1 for i in range(n)], n))
l_p = tuple(map(int, input().split()))
l_q = tuple(map(int, input().split()))
res = abs(all_.index(l_p) - all_.index(l_q))
print(res)

-from numpy import prod +from itertools import permutations - - -def solve(l: list): -    now = [i + 1 for i in range(n)] -    res = 0 -    for i in range(n): -        res += now.index(l[i]) * prod([j + 1 for j in range(len(now) - 1)]) -        now.remove(l[i]) -    return res - - -l_p = list(map(int, input().split())) -l_q = list(map(int, input().split())) -print((int(abs(solve(l_p) - solve(l_q))))) +all_ = list(permutations([i + 1 for i in range(n)], n)) +l_p = tuple(map(int, input().split())) +l_q = tuple(map(int, input().split())) +res = abs(all_.index(l_p) - all_.index(l_q)) +print(res)