#python3

from itertools import permutations

n=int(eval(input()))

p=[int(i) for i in input().split()]

q=[int(i) for i in input().split()]



def f(lis):

  cnt=0

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

    cnt+=1

    if i == tuple(lis):

      return cnt



print((abs(f(p)-f(q))))

def main():

    n = int(eval(input()))

    p = tuple(int(i) for i in input().split())

    q = tuple(int(i) for i in input().split())

    from itertools import permutations 

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

    o1 = -1

    o2 = -1

    cnt = 0

    for i in permutations(a):

      cnt += 1

      if i == p:

          o1 = cnt

      if i == q:

          o2 = cnt

      ans = abs(o1-o2)

    print(ans)

main()
# python3
from itertools import permutations

n = int(eval(input()))
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]


def f(lis):
    cnt = 0
    for i in permutations(list(range(1, n + 1))):
        cnt += 1
        if i == tuple(lis):
            return cnt


print((abs(f(p) - f(q))))

def main():
    n = int(eval(input()))
    p = tuple(int(i) for i in input().split())
    q = tuple(int(i) for i in input().split())
    from itertools import permutations

    a = [i for i in range(1, n + 1)]
    o1 = -1
    o2 = -1
    cnt = 0
    for i in permutations(a):
        cnt += 1
        if i == p:
            o1 = cnt
        if i == q:
            o2 = cnt
        ans = abs(o1 - o2)
    print(ans)


main()

-# python3 -from itertools import permutations +def main(): +    n = int(eval(input())) +    p = tuple(int(i) for i in input().split()) +    q = tuple(int(i) for i in input().split()) +    from itertools import permutations -n = int(eval(input())) -p = [int(i) for i in input().split()] -q = [int(i) for i in input().split()] +    a = [i for i in range(1, n + 1)] +    o1 = -1 +    o2 = -1 +    cnt = 0 +    for i in permutations(a): +        cnt += 1 +        if i == p: +            o1 = cnt +        if i == q: +            o2 = cnt +        ans = abs(o1 - o2) +    print(ans) -def f(lis): -    cnt = 0 -    for i in permutations(list(range(1, n + 1))): -        cnt += 1 -        if i == tuple(lis): -            return cnt - - -print((abs(f(p) - f(q)))) +main()