# -*- coding: utf-8 -*-

"""

Created on Thu May  7 03:09:14 2020



@author: Kanaru Sato

"""

import itertools as it



n = int(eval(input()))

l = []

for i in range(1,n+1):

    l.append(i)

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

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



count = 0

for sequence in it.permutations(l,n):

    if list(sequence) == p:

        pnum = count

    if list(sequence) == q:

        qnum = count

    count += 1



print((abs(pnum-qnum)))
# -*- coding: utf-8 -*-

"""

Created on Sun May 17 19:16:09 2020



@author: Kanaru Sato

"""

import itertools



n = int(eval(input()))

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

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



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



count = 0

for perm in itertools.permutations(nums):

    count += 1

    if p == perm:

        pcount = count

    if q == perm:

        qcount = count



print((abs(pcount-qcount)))
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 03:09:14 2020
@author: Kanaru Sato
"""
import itertools as it

n = int(eval(input()))
l = []
for i in range(1, n + 1):
    l.append(i)
p = list(map(int, input().split()))
q = list(map(int, input().split()))
count = 0
for sequence in it.permutations(l, n):
    if list(sequence) == p:
        pnum = count
    if list(sequence) == q:
        qnum = count
    count += 1
print((abs(pnum - qnum)))

# -*- coding: utf-8 -*-
"""
Created on Sun May 17 19:16:09 2020
@author: Kanaru Sato
"""
import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
nums = [i + 1 for i in range(n)]
count = 0
for perm in itertools.permutations(nums):
    count += 1
    if p == perm:
        pcount = count
    if q == perm:
        qcount = count
print((abs(pcount - qcount)))

-Created on Thu May  7 03:09:14 2020 +Created on Sun May 17 19:16:09 2020 -import itertools as it +import itertools -l = [] -for i in range(1, n + 1): -    l.append(i) -p = list(map(int, input().split())) -q = list(map(int, input().split())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +nums = [i + 1 for i in range(n)] -for sequence in it.permutations(l, n): -    if list(sequence) == p: -        pnum = count -    if list(sequence) == q: -        qnum = count +for perm in itertools.permutations(nums): -print((abs(pnum - qnum))) +    if p == perm: +        pcount = count +    if q == perm: +        qcount = count +print((abs(pcount - qcount)))