import itertools

n=int(eval(input()))

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

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

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

nc=len(x)



for i in range(nc):

  if x[i]==p:

    pn=i

    

for j in range(nc):

  if x[j]==q:

    qn=j





print((abs(pn-qn)))
def main():

    from itertools import permutations

    import sys

    input=sys.stdin.readline

    n=int(eval(input()))

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

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

    

    cnt=1

    for i in permutations(list(range(1,n+1))):

        if i==p:

            pi=cnt

        if i==q:

            qi=cnt

        cnt+=1

    

    print((abs(pi-qi)))

if __name__=='__main__':

    main()
import itertools

n = int(eval(input()))
p = tuple([int(i) for i in input().split()])
q = tuple([int(i) for i in input().split()])
x = list(itertools.permutations(list(range(1, n + 1))))
nc = len(x)
for i in range(nc):
    if x[i] == p:
        pn = i
for j in range(nc):
    if x[j] == q:
        qn = j
print((abs(pn - qn)))

def main():
    from itertools import permutations
    import sys

    input = sys.stdin.readline
    n = int(eval(input()))
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    cnt = 1
    for i in permutations(list(range(1, n + 1))):
        if i == p:
            pi = cnt
        if i == q:
            qi = cnt
        cnt += 1
    print((abs(pi - qi)))


if __name__ == "__main__":
    main()

-import itertools +def main(): +    from itertools import permutations +    import sys -n = int(eval(input())) -p = tuple([int(i) for i in input().split()]) -q = tuple([int(i) for i in input().split()]) -x = list(itertools.permutations(list(range(1, n + 1)))) -nc = len(x) -for i in range(nc): -    if x[i] == p: -        pn = i -for j in range(nc): -    if x[j] == q: -        qn = j -print((abs(pn - qn))) +    input = sys.stdin.readline +    n = int(eval(input())) +    p = tuple(map(int, input().split())) +    q = tuple(map(int, input().split())) +    cnt = 1 +    for i in permutations(list(range(1, n + 1))): +        if i == p: +            pi = cnt +        if i == q: +            qi = cnt +        cnt += 1 +    print((abs(pi - qi))) + + +if __name__ == "__main__": +    main()