# https://atcoder.jp/contests/abc150/tasks/abc150_c

import sys

from itertools import *





def S(): return sys.stdin.readline().rstrip()





def I(): return int(sys.stdin.readline().rstrip())





def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))





def LS(): return list(sys.stdin.readline().rstrip().split())





N = I()

p = tuple(LI())

q = tuple(LI())



all = list(range(1, N+1))



all = list(permutations(all, N))





a = (all.index(p))

b = (all.index(q))



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

import itertools



def I(): return int(sys.stdin.readline().rstrip())

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

def S(): return sys.stdin.readline().rstrip()

def LS(): return list(sys.stdin.readline().rstrip().split())



N = I()

P = tuple(LI())

Q = tuple(LI())



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



p_index = li.index(P)

q_index = li.index(Q)

print((abs(p_index-q_index)))

# https://atcoder.jp/contests/abc150/tasks/abc150_c
import sys
from itertools import *


def S():
    return sys.stdin.readline().rstrip()


def I():
    return int(sys.stdin.readline().rstrip())


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


def LS():
    return list(sys.stdin.readline().rstrip().split())


N = I()
p = tuple(LI())
q = tuple(LI())
all = list(range(1, N + 1))
all = list(permutations(all, N))
a = all.index(p)
b = all.index(q)
print((abs(a - b)))

import sys
import itertools


def I():
    return int(sys.stdin.readline().rstrip())


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


def S():
    return sys.stdin.readline().rstrip()


def LS():
    return list(sys.stdin.readline().rstrip().split())


N = I()
P = tuple(LI())
Q = tuple(LI())
li = list(itertools.permutations(list(range(1, N + 1))))
p_index = li.index(P)
q_index = li.index(Q)
print((abs(p_index - q_index)))

-# https://atcoder.jp/contests/abc150/tasks/abc150_c -from itertools import * - - -def S(): -    return sys.stdin.readline().rstrip() +import itertools +def S(): +    return sys.stdin.readline().rstrip() + + -p = tuple(LI()) -q = tuple(LI()) -all = list(range(1, N + 1)) -all = list(permutations(all, N)) -a = all.index(p) -b = all.index(q) -print((abs(a - b))) +P = tuple(LI()) +Q = tuple(LI()) +li = list(itertools.permutations(list(range(1, N + 1)))) +p_index = li.index(P) +q_index = li.index(Q) +print((abs(p_index - q_index)))