import itertools


N = int(input())
P = input()
Q = input()

a = -1
b = -1
for i, pattern in enumerate(itertools.permutations(range(1, N + 1))):
    p = " ".join(map(str, pattern))
    if p == P:
        a = i
    if p == Q:
        b = i

print(abs(a - b))
