import sys

import itertools

def input():

    return sys.stdin.readline()[:-1]

n=int(eval(input()))

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

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

li=[]

for i in range(n):

    li.append(i+1)

a=list(itertools.permutations(li))

tmp=[0,0]



for i in range(len(a)):

    if list(a[i])==p:

        tmp[0]=i

    if list(a[i])==q:

        tmp[1]=i

print((abs(tmp[0]-tmp[1])))
import bisect, copy, heapq, math, sys

from collections import *

from functools import lru_cache

from itertools import accumulate, combinations, permutations, product

def input():

    return sys.stdin.readline()[:-1]

def ruiseki(lst):

    return [0]+list(accumulate(lst))

def celi(a,b):

    return -(-a//b)

sys.setrecursionlimit(5000000)

mod=pow(10,9)+7

al=[chr(ord('a') + i) for i in range(26)]

direction=[[1,0],[0,1],[-1,0],[0,-1]]



n=int(eval(input()))

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

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



for i in range(n):

    p[i]-=1

    q[i]-=1



com=list(permutations(list(range(n))))

# print(p,q)

a=0

b=0

for i in range(len(com)):

    # print(com[i])

    if com[i]==tuple(p):

        a=i

    if com[i]==tuple(q):

        b=i

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


def input():
    return sys.stdin.readline()[:-1]


n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
li = []
for i in range(n):
    li.append(i + 1)
a = list(itertools.permutations(li))
tmp = [0, 0]
for i in range(len(a)):
    if list(a[i]) == p:
        tmp[0] = i
    if list(a[i]) == q:
        tmp[1] = i
print((abs(tmp[0] - tmp[1])))

import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product


def input():
    return sys.stdin.readline()[:-1]


def ruiseki(lst):
    return [0] + list(accumulate(lst))


def celi(a, b):
    return -(-a // b)


sys.setrecursionlimit(5000000)
mod = pow(10, 9) + 7
al = [chr(ord("a") + i) for i in range(26)]
direction = [[1, 0], [0, 1], [-1, 0], [0, -1]]
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
for i in range(n):
    p[i] -= 1
    q[i] -= 1
com = list(permutations(list(range(n))))
# print(p,q)
a = 0
b = 0
for i in range(len(com)):
    # print(com[i])
    if com[i] == tuple(p):
        a = i
    if com[i] == tuple(q):
        b = i
print((abs(a - b)))

-import sys -import itertools +import bisect, copy, heapq, math, sys +from collections import * +from functools import lru_cache +from itertools import accumulate, combinations, permutations, product +def ruiseki(lst): +    return [0] + list(accumulate(lst)) + + +def celi(a, b): +    return -(-a // b) + + +sys.setrecursionlimit(5000000) +mod = pow(10, 9) + 7 +al = [chr(ord("a") + i) for i in range(26)] +direction = [[1, 0], [0, 1], [-1, 0], [0, -1]] -li = [] -    li.append(i + 1) -a = list(itertools.permutations(li)) -tmp = [0, 0] -for i in range(len(a)): -    if list(a[i]) == p: -        tmp[0] = i -    if list(a[i]) == q: -        tmp[1] = i -print((abs(tmp[0] - tmp[1]))) +    p[i] -= 1 +    q[i] -= 1 +com = list(permutations(list(range(n)))) +# print(p,q) +a = 0 +b = 0 +for i in range(len(com)): +    # print(com[i]) +    if com[i] == tuple(p): +        a = i +    if com[i] == tuple(q): +        b = i +print((abs(a - b)))