import itertools

import math



N = int(eval(input()))

P = int(''.join(input().split()))

Q = int(''.join(input().split()))



N_list = []

for i in itertools.permutations(list(range(1,N+1))):

    N_list.append(int(''.join([str(x) for x in i])))



pointer1 = 0

pointer2 = 0



for i,x in enumerate(N_list):

  if x == P:

    pointer1 = i

  if x == Q:

    pointer2 = i

    

print((abs(pointer1 - pointer2)))
import itertools



n = int(eval(input()))

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

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



a,b = 0,0

for i,x in enumerate(itertools.permutations(list(range(1,n+1)))):



    if list(x) == p:

        a = i

    if list(x) == q:

        b = i

        

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

N = int(eval(input()))
P = int("".join(input().split()))
Q = int("".join(input().split()))
N_list = []
for i in itertools.permutations(list(range(1, N + 1))):
    N_list.append(int("".join([str(x) for x in i])))
pointer1 = 0
pointer2 = 0
for i, x in enumerate(N_list):
    if x == P:
        pointer1 = i
    if x == Q:
        pointer2 = i
print((abs(pointer1 - pointer2)))

import itertools

n = int(eval(input()))
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
a, b = 0, 0
for i, x in enumerate(itertools.permutations(list(range(1, n + 1)))):
    if list(x) == p:
        a = i
    if list(x) == q:
        b = i
print((abs(a - b)))

-import math -N = int(eval(input())) -P = int("".join(input().split())) -Q = int("".join(input().split())) -N_list = [] -for i in itertools.permutations(list(range(1, N + 1))): -    N_list.append(int("".join([str(x) for x in i]))) -pointer1 = 0 -pointer2 = 0 -for i, x in enumerate(N_list): -    if x == P: -        pointer1 = i -    if x == Q: -        pointer2 = i -print((abs(pointer1 - pointer2))) +n = int(eval(input())) +p = [int(x) for x in input().split()] +q = [int(x) for x in input().split()] +a, b = 0, 0 +for i, x in enumerate(itertools.permutations(list(range(1, n + 1)))): +    if list(x) == p: +        a = i +    if list(x) == q: +        b = i +print((abs(a - b)))