import itertools



n = int(eval(input()))

l = sorted(list(itertools.permutations([i for i in range(1,n+1)])))



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

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





a,b = 0,0





if al == bl:

    print((0))

else:

    for idx,i in enumerate(l):

        if i == al:

            a = idx

        elif i == bl:

            b = idx

        else:

            pass

        

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



n = int(eval(input()))

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

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



all_list = list(itertools.permutations(list(range(1,n+1))))



print((abs(all_list.index(pl) - all_list.index(ql))))

import itertools

n = int(eval(input()))
l = sorted(list(itertools.permutations([i for i in range(1, n + 1)])))
al = tuple([int(i) for i in input().split()])
bl = tuple([int(i) for i in input().split()])
a, b = 0, 0
if al == bl:
    print((0))
else:
    for idx, i in enumerate(l):
        if i == al:
            a = idx
        elif i == bl:
            b = idx
        else:
            pass
    print((abs(a - b)))

import itertools

n = int(eval(input()))
pl = tuple([int(i) for i in input().split()])
ql = tuple([int(i) for i in input().split()])
all_list = list(itertools.permutations(list(range(1, n + 1))))
print((abs(all_list.index(pl) - all_list.index(ql))))

-l = sorted(list(itertools.permutations([i for i in range(1, n + 1)]))) -al = tuple([int(i) for i in input().split()]) -bl = tuple([int(i) for i in input().split()]) -a, b = 0, 0 -if al == bl: -    print((0)) -else: -    for idx, i in enumerate(l): -        if i == al: -            a = idx -        elif i == bl: -            b = idx -        else: -            pass -    print((abs(a - b))) +pl = tuple([int(i) for i in input().split()]) +ql = tuple([int(i) for i in input().split()]) +all_list = list(itertools.permutations(list(range(1, n + 1)))) +print((abs(all_list.index(pl) - all_list.index(ql))))