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

from itertools import permutations



N = int(input().strip())

pat_P = [ "".join( input().rstrip().split() )][0]

pat_Q = [ "".join( input().rstrip().split() )][0]

#-----



perm_list = [ "".join( map(str, pat) ) for pat in permutations(list(range(1,N+1)))]

perm_list.sort()



a = perm_list.index(pat_P) + 1

b = perm_list.index(pat_Q) + 1

print((abs(a-b)))

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

from itertools import permutations



N = int(input().strip())

pat_P = tuple(map(int, input().rstrip().split()))

pat_Q = tuple(map(int, input().rstrip().split()))

#-----



perm_list = [ pat for pat in permutations(list(range(1,N+1)))]



a = perm_list.index(pat_P) + 1

b = perm_list.index(pat_Q) + 1

print((abs(a-b)))

# -*- coding: utf-8 -*-
from itertools import permutations

N = int(input().strip())
pat_P = ["".join(input().rstrip().split())][0]
pat_Q = ["".join(input().rstrip().split())][0]
# -----
perm_list = ["".join(map(str, pat)) for pat in permutations(list(range(1, N + 1)))]
perm_list.sort()
a = perm_list.index(pat_P) + 1
b = perm_list.index(pat_Q) + 1
print((abs(a - b)))

# -*- coding: utf-8 -*-
from itertools import permutations

N = int(input().strip())
pat_P = tuple(map(int, input().rstrip().split()))
pat_Q = tuple(map(int, input().rstrip().split()))
# -----
perm_list = [pat for pat in permutations(list(range(1, N + 1)))]
a = perm_list.index(pat_P) + 1
b = perm_list.index(pat_Q) + 1
print((abs(a - b)))

-pat_P = ["".join(input().rstrip().split())][0] -pat_Q = ["".join(input().rstrip().split())][0] +pat_P = tuple(map(int, input().rstrip().split())) +pat_Q = tuple(map(int, input().rstrip().split())) -perm_list = ["".join(map(str, pat)) for pat in permutations(list(range(1, N + 1)))] -perm_list.sort() +perm_list = [pat for pat in permutations(list(range(1, N + 1)))]