import itertools



def solve():

    N = int(eval(input()))

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

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

    l = [i for i in range(1, N+1)]

    l = list(itertools.permutations(l))

    

    a = 0

    for k, v in enumerate(l):

        if A == list(v):

            a = k+1

        

    b = 0

    for k, v in enumerate(l):

        if B == list(v):

            b = k+1

    return abs(a-b)



print((solve()))
import itertools



def solve():

    N = int(eval(input()))

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

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

    l = [i for i in range(1, N+1)]

    l = list(itertools.permutations(l))

    

    a = 0

    b = 0

    for k, v in enumerate(l):

        v = list(v)

        if v == A:

            a = k+1

        if v == B:

            b = k+1

    

    return abs(a-b)



print((solve()))
import itertools


def solve():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [i for i in range(1, N + 1)]
    l = list(itertools.permutations(l))
    a = 0
    for k, v in enumerate(l):
        if A == list(v):
            a = k + 1
    b = 0
    for k, v in enumerate(l):
        if B == list(v):
            b = k + 1
    return abs(a - b)


print((solve()))

import itertools


def solve():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [i for i in range(1, N + 1)]
    l = list(itertools.permutations(l))
    a = 0
    b = 0
    for k, v in enumerate(l):
        v = list(v)
        if v == A:
            a = k + 1
        if v == B:
            b = k + 1
    return abs(a - b)


print((solve()))

-    for k, v in enumerate(l): -        if A == list(v): -            a = k + 1 -        if B == list(v): +        v = list(v) +        if v == A: +            a = k + 1 +        if v == B: