from collections import deque

from collections import Counter

from itertools import product, permutations,combinations

from operator import itemgetter

from heapq import heappop, heappush

from bisect import bisect_left, bisect_right, bisect

#pypyではscipy, numpyは使えない

#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree

#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix

#import numpy as np

from fractions import gcd

from math import ceil,floor, sqrt, cos, sin, pi, factorial

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10**8)

INF = float('inf')

from datetime import datetime as dt



def main():

  n = int(eval(input()))

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

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

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

  for i, junretsu in enumerate(permutations(l, n)):

    if junretsu==p:

      a = i

    if junretsu==q:

      b=i

  print((abs(b-a)))

  

  

if __name__ == '__main__':

  main()
import sys

read = sys.stdin.read

readline = sys.stdin.buffer.readline

from itertools import permutations

sys.setrecursionlimit(10 ** 8)

INF = float('inf')

MOD = 10 ** 9 + 7





def main():

    N = int(readline())

    P = tuple(map(int, readline().split()))

    Q = tuple(map(int, readline().split()))





    for i, A in enumerate(permutations(list(range(1,N+1)))):

        if A == P:

            p = i

        if A == Q:

            q = i



    print((abs(p-q)))









if __name__ == '__main__':

    main()

from collections import deque
from collections import Counter
from itertools import product, permutations, combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect

# pypyではscipy, numpyは使えない
# from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
# from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
# import numpy as np
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi, factorial
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float("inf")
from datetime import datetime as dt


def main():
    n = int(eval(input()))
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = [i for i in range(1, n + 1)]
    for i, junretsu in enumerate(permutations(l, n)):
        if junretsu == p:
            a = i
        if junretsu == q:
            b = i
    print((abs(b - a)))


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
from itertools import permutations

sys.setrecursionlimit(10**8)
INF = float("inf")
MOD = 10**9 + 7


def main():
    N = int(readline())
    P = tuple(map(int, readline().split()))
    Q = tuple(map(int, readline().split()))
    for i, A in enumerate(permutations(list(range(1, N + 1)))):
        if A == P:
            p = i
        if A == Q:
            q = i
    print((abs(p - q)))


if __name__ == "__main__":
    main()

-from collections import deque -from collections import Counter -from itertools import product, permutations, combinations -from operator import itemgetter -from heapq import heappop, heappush -from bisect import bisect_left, bisect_right, bisect - -# pypyではscipy, numpyは使えない -# from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree -# from scipy.sparse import csr_matrix, coo_matrix, lil_matrix -# import numpy as np -from fractions import gcd -from math import ceil, floor, sqrt, cos, sin, pi, factorial -read = sys.stdin.buffer.read +read = sys.stdin.read -readlines = sys.stdin.buffer.readlines +from itertools import permutations + -from datetime import datetime as dt +MOD = 10**9 + 7 -    n = int(eval(input())) -    p = tuple(map(int, input().split())) -    q = tuple(map(int, input().split())) -    l = [i for i in range(1, n + 1)] -    for i, junretsu in enumerate(permutations(l, n)): -        if junretsu == p: -            a = i -        if junretsu == q: -            b = i -    print((abs(b - a))) +    N = int(readline()) +    P = tuple(map(int, readline().split())) +    Q = tuple(map(int, readline().split())) +    for i, A in enumerate(permutations(list(range(1, N + 1)))): +        if A == P: +            p = i +        if A == Q: +            q = i +    print((abs(p - q)))