import sys, bisect, math, itertools, string, queue, copy

import numpy as np

import scipy

from collections import Counter,defaultdict,deque

from itertools import permutations, combinations

from heapq import heappop, heappush

# input = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

def inp(): return int(eval(input()))

def inpm(): return list(map(int,input().split()))

def inpl(): return list(map(int, input().split()))

def inpls(): return list(input().split())

def inplm(n): return list(int(eval(input())) for _ in range(n))

def inplL(n): return [list(eval(input())) for _ in range(n)]

def inplT(n): return [tuple(eval(input())) for _ in range(n)]

def inpll(n): return [list(map(int, input().split())) for _ in range(n)]

def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])



n = inp()

A = inpl()

B = inpl()

aa = 0

bb = 0

i = 0

for tmp in itertools.permutations(list(range(1,n+1))):

    i += 1

    if list(tmp) == A:

        aa = i

    if list(tmp) == B:

        bb = i

print((abs(aa - bb)))
import sys, bisect, math, itertools, string, queue, copy

import numpy as np

import scipy

from collections import Counter,defaultdict,deque

from itertools import permutations, combinations

from heapq import heappop, heappush

# input = sys.stdin.readline.rstrip()

sys.setrecursionlimit(10**8)

mod = 10**9+7

def inp(): return int(eval(input()))

def inpm(): return list(map(int,input().split()))

def inpl(): return tuple(map(int, input().split()))

def inpls(): return list(input().split())

def inplm(n): return list(int(eval(input())) for _ in range(n))

def inplL(n): return [list(eval(input())) for _ in range(n)]

def inplT(n): return [tuple(eval(input())) for _ in range(n)]

def inpll(n): return [list(map(int, input().split())) for _ in range(n)]

def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]

def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])



n = inp()

P = inpl()

Q = inpl()



i = 0

a = 0

b = 0

for p in permutations(list(range(1,n+1))):

    i += 1

    if p == P:

        a = i

    if p == Q:

        b = i



print((abs(a-b)))
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter, defaultdict, deque
from itertools import permutations, combinations
from heapq import heappop, heappush

# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7


def inp():
    return int(eval(input()))


def inpm():
    return list(map(int, input().split()))


def inpl():
    return list(map(int, input().split()))


def inpls():
    return list(input().split())


def inplm(n):
    return list(int(eval(input())) for _ in range(n))


def inplL(n):
    return [list(eval(input())) for _ in range(n)]


def inplT(n):
    return [tuple(eval(input())) for _ in range(n)]


def inpll(n):
    return [list(map(int, input().split())) for _ in range(n)]


def inplls(n):
    return sorted([list(map(int, input().split())) for _ in range(n)])


n = inp()
A = inpl()
B = inpl()
aa = 0
bb = 0
i = 0
for tmp in itertools.permutations(list(range(1, n + 1))):
    i += 1
    if list(tmp) == A:
        aa = i
    if list(tmp) == B:
        bb = i
print((abs(aa - bb)))

import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter, defaultdict, deque
from itertools import permutations, combinations
from heapq import heappop, heappush

# input = sys.stdin.readline.rstrip()
sys.setrecursionlimit(10**8)
mod = 10**9 + 7


def inp():
    return int(eval(input()))


def inpm():
    return list(map(int, input().split()))


def inpl():
    return tuple(map(int, input().split()))


def inpls():
    return list(input().split())


def inplm(n):
    return list(int(eval(input())) for _ in range(n))


def inplL(n):
    return [list(eval(input())) for _ in range(n)]


def inplT(n):
    return [tuple(eval(input())) for _ in range(n)]


def inpll(n):
    return [list(map(int, input().split())) for _ in range(n)]


def inplt(n):
    return [tuple(map(int, input().split())) for _ in range(n)]


def inplls(n):
    return sorted([list(map(int, input().split())) for _ in range(n)])


n = inp()
P = inpl()
Q = inpl()
i = 0
a = 0
b = 0
for p in permutations(list(range(1, n + 1))):
    i += 1
    if p == P:
        a = i
    if p == Q:
        b = i
print((abs(a - b)))

-# input = sys.stdin.readline +# input = sys.stdin.readline.rstrip() -    return list(map(int, input().split())) +    return tuple(map(int, input().split())) +def inplt(n): +    return [tuple(map(int, input().split())) for _ in range(n)] + + -A = inpl() -B = inpl() -aa = 0 -bb = 0 +P = inpl() +Q = inpl() -for tmp in itertools.permutations(list(range(1, n + 1))): +a = 0 +b = 0 +for p in permutations(list(range(1, n + 1))): -    if list(tmp) == A: -        aa = i -    if list(tmp) == B: -        bb = i -print((abs(aa - bb))) +    if p == P: +        a = i +    if p == Q: +        b = i +print((abs(a - b)))