CodeEff/ECCO huggingface dataset:
# Original solution
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N = II()
    P = input().replace(' ','')
    Q = input().replace(' ','')

    kouho = set(range(1,N+1))
    def kaijo(n):
        ans = 1
        for i in range(1,n+1):
            ans *= i
        return ans
    numbers = [0] * kaijo(N)
    # print(len(numbers))
    from copy import copy
    def mkp(kouho,i,p):
        if i == N-1:
            numbers.append(p+kouho[0])
            return

        for j in range(len(kouho)):
            kouho2 = copy(kouho)
            kouho2.remove(kouho[j])
            mkp(kouho2,i+1,p+kouho[j])
    mkp(list(map(str,range(1,N+1))),0,'')
    numbers = numbers[kaijo(N):]


    print(abs(numbers.index(P)-numbers.index(Q)))
    



   




if __name__ == '__main__':
    main()


# Optimized solution
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N = II()
    P = LMIIS()
    Q = LMIIS()
    from math import factorial

    def index(P):
        candidates = list(range(1,N+1))
        ans = 0
        for i in range(N-1):
            ans += candidates.index(P[i]) * factorial(N-1-i)
            candidates.remove(P[i])
        return ans + 1

    print(abs(index(P)-index(Q)))

    



   




if __name__ == '__main__':
    main()
# Original solution
from itertools import permutations

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

iP = -1
iQ = -1
for idx, perm in enumerate(permutations(list(range(1, N+1)))):
    lp = list(perm)
    if lp == P:
        iP = idx
    if lp == Q:
        iQ = idx
print((abs(iP - iQ)))

# Optimized solution
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

iP, iQ = None, None
for idx, perm in enumerate(permutations(list(range(1, N + 1)))):
    if perm == P:
        iP = idx
    if perm == Q:
        iQ = idx
print((abs(iP - iQ)))
# Original solution
# ABC150C - Count Order
from itertools import permutations


def main():
    N, *PQ = list(map(int, open(0).read().split()))
    P, Q = PQ[:N], PQ[N:]
    a, b = 0, 0
    for i, perm in enumerate(permutations(list(range(1, N + 1)), N), 1):
        perm = list(perm)
        if perm == P:
            a = i
        if perm == Q:
            b = i
    ans = abs(a - b)
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
# ABC150C - Count Order
from itertools import permutations


def main():
    N = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    perms = list(permutations(list(range(1, N + 1))))
    ans = abs(perms.index(P) - perms.index(Q))
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
import itertools

if __name__ == '__main__':
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(eval(input()))
    p = input().split(' ')
    q = input().split(' ')

    for i in range(0, len(q)):
        p[i] = int(p[i])
        q[i] = int(q[i])

    realnums = p.copy()

    realnums.sort()

    perm = list(itertools.permutations(realnums))

    it = 0

    qplace = 0
    pplace = 0

    for i in perm:
        if list(i) == q:
            qplace = it
        if list(i) == p:
            pplace = it
        it += 1
    print((abs(qplace - pplace)))

# Optimized solution
import itertools

if __name__ == '__main__':
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(eval(input()))
    p = list(map(int, input().split(' ')))
    q = list(map(int, input().split(' ')))

    realnums = p.copy()

    realnums.sort()

    perm = list(map(list, itertools.permutations(realnums)))

    print((abs(perm.index(q) - perm.index(p))))
# Original solution
import itertools
(n,),p,q=[tuple(map(int, i.split())) for i in open(0)]
j=list(itertools.permutations(list(range(1, -~n))))
print((abs(j.index(p) - j.index(q))))

# Optimized solution
import math
(n,),p,q=[list(map(int, i.split())) for i in open(0)]
def c(l):
    s=0
    t=list(range(1,n+1))
    for i in range(n):
        s+=t.index(l[i])*math.factorial(n-i-1)
        t.remove(l[i])
    return s
print((abs(c(p)-c(q))))
# Original solution
import itertools

def solve():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [i for i in range(1, N+1)]
    l = list(itertools.permutations(l))
    
    a = 0
    for k, v in enumerate(l):
        if A == list(v):
            a = k+1
        
    b = 0
    for k, v in enumerate(l):
        if B == list(v):
            b = k+1
    return abs(a-b)

print((solve()))

# Optimized solution
import itertools

def solve():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [i for i in range(1, N+1)]
    l = list(itertools.permutations(l))
    
    a = 0
    b = 0
    for k, v in enumerate(l):
        v = list(v)
        if v == A:
            a = k+1
        if v == B:
            b = k+1
    
    return abs(a-b)

print((solve()))
# Original solution
import itertools
N=int(eval(input()))
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
list_a = list(range(1,N+1))
tuple_a = tuple(list_a)
ptr = list(itertools.permutations(tuple_a))
ptr_num = len(list(itertools.permutations(tuple_a)))
for i in range(0,ptr_num):
  if ptr[i] == tuple(P):
    P_num=i+1
  if ptr[i] == tuple(Q):
    Q_num=i+1
print((abs(P_num-Q_num)))


# Optimized solution
import itertools
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
jyunretsu=list(itertools.permutations(list(range(1,n+1))))
print((abs(jyunretsu.index(p)-jyunretsu.index(q))))
# Original solution
import itertools
n = int(eval(input()))
p = list(map(int, input().split()))
pp = "".join((str(n) for n in p))
q = list(map(int, input().split()))
qq = "".join((str(n) for n in q))
a = [i+1 for i in range(n)]
b = 0
c = 0
d = 0
for i in itertools.permutations(a):
	d += 1
	x = "".join((str(n) for n in i))
	if pp == x:
		b = d
	if qq == x:
		c = d
print((abs(b-c)))

# Optimized solution
import itertools
n = int(eval(input()))
p = list(map(int, input().split()))
pp = "".join([str(i) for i in p])
q = list(map(int, input().split()))
qq = "".join([str(i) for i in q])
x = [i+1 for i in range(n)]
z = []
for v in itertools.permutations(x):
	y = "".join([str(i) for i in v])
	z.append(y)
a = z.index(pp)
b = z.index(qq)
print((abs(a-b)))
# Original solution
# coding UTF-8
# 
# problem C
# 

import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

seq = tuple(range(1,N+1))
seqlist = list(itertools.permutations(seq))

#print(seqlist)
#print(seqlist.index(P))
#print(seqlist.index(Q))

print((abs(seqlist.index(P)-abs(seqlist.index(Q)))))


# Optimized solution
import sys
import itertools

readline = sys.stdin.readline

def main():
    N = int(eval(input()))
    P = tuple(map(int, readline().split()))
    Q = tuple(map(int, readline().split()))
    
    dictN = list(itertools.permutations(list(range(1, N+1))))
    p = dictN.index(P)
    q = dictN.index(Q)
    ans = abs(p-q)
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
r = int(eval(input()))
tp = tuple(map(int,input().split()))
tq = tuple(map(int,input().split()))

import itertools

p, q = 0, 0 
for i, ti in enumerate(itertools.permutations(list(range(1,r+1)))):
    if tp == ti:
        p = i        
    if tq == ti:
        q = i
    if p * q > 0:
        break

print((abs(p-q)))

# Optimized solution
n = int(eval(input()))
tp = tuple(map(int, input().split()))
tq = tuple(map(int, input().split()))

import itertools as it

l = list(it.permutations(list(range(1, n+1))))

print((abs(l.index(tp) - l.index(tq))))
# Original solution
n = int(eval(input()))
tp = tuple(map(int, input().split()))
tq = tuple(map(int, input().split()))

import itertools as it

l = list(it.permutations(list(range(1, n+1))))

print((abs(l.index(tp) - l.index(tq))))

# Optimized solution
f=lambda:tuple(map(int,input().split()))
n=int(eval(input()))
tp,tq=f(),f()
import itertools as it
l=list(it.permutations(list(range(1,n+1))))
print((abs(l.index(tp)-l.index(tq))))
# Original solution
n=int(eval(input()))
from itertools import *
p=[*permutations(list(range(1,n+1)))]
f=lambda:p.index(tuple(map(int,input().split())))
print((abs(f()-f())))

# Optimized solution
f=lambda:tuple(map(int,input().split()))
n=f()[0]
from itertools import *
p=[*permutations(list(range(1,n+1)))].index
print((abs(p(f())-p(f()))))
# Original solution
import itertools
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
arrs=list(itertools.permutations(list(range(1,n+1))))
cnt1=0
cnt2=0
for i in range(len(arrs)):
  if arrs[i]==p:
    cnt1=i
  if arrs[i]==q:
    cnt2=i
print((abs(cnt2-cnt1)))

# Optimized solution
def fact(n):
  ret=1
  for i in range(2,n+1):
    ret=ret*i
  return ret

n=int(eval(input()))
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
cnt1=0
cnt2=0
checked1=[0]*(n+1)
checked2=[0]*(n+1)
for i in range(n):
  checked1[arr1[i]]=1
  checked2[arr2[i]]=1
  cnt1+=(arr1[i]-1-sum(checked1[:arr1[i]]))*fact(n-i-1)
  cnt2+=(arr2[i]-1-sum(checked2[:arr2[i]]))*fact(n-i-1)
print((abs(cnt1-cnt2)))
# Original solution
import itertools
N = int(eval(input()))
NL = [str(i) for i in range(1,N+1)]
P = input().replace(' ', '')
Q = input().replace(' ', '')
NLL = sorted(list(itertools.permutations(NL,N)))
a = 0
b = 0
for i in range(len(NLL)):
    if ''.join(list(NLL[i])) == P:
        a = i+1
    if ''.join(list(NLL[i])) == Q:
        b = i+1
print((abs(a-b)))

# Optimized solution
import itertools
import math
N = int(eval(input()))
P = ''.join(input().split())
Q = ''.join(input().split())
np = sorted(list([''.join(x) for x in itertools.permutations([str(i+1) for i in range(N)])]))
print((abs((np.index(P)+1)-(np.index(Q)+1))))
# Original solution
import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
l = []
c = []
ls = []
for i in range(N):
    l.append(i + 1)
    c.append(0)

# 重複なしのあらゆる並び
l = list(map(list, itertools.permutations(l)))
print((abs(l.index(P) - l.index(Q))))


# Optimized solution
import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
l = []
for i in range(N):
    l.append(i + 1)

# 重複なしのあらゆる並び(itertools.permutations)
l = list(map(list, itertools.permutations(l)))
print((abs(l.index(P) - l.index(Q))))
# Original solution
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)))

# Optimized solution
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))))
# Original solution
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))

t = [0]*8
t[1] = 1
for i in range(2,n):
  t[i] = i * t[i - 1]

a = 0
l = list(range(1, n+1))
for i in range(n):
  a += l.index(p[i]) * t[len(l) - 1]
  l.remove(p[i])

b = 0
l = list(range(1, n+1))
for i in range(n):
  b += l.index(q[i]) * t[len(l) - 1]
  l.remove(q[i])

print((abs(a - b)))

# Optimized solution
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))

s = [0]*(n+1)
s[1] = 1
for i in range(2, n+1):
  s[i] = s[i-1]*i

x = 0
l = [i for i in range(1, n+1)]
for i in p:
  x += l.index(i) * s[len(l) - 1]
  l.remove(i)

y = 0
l = [i for i in range(1, n+1)]
for i in q:
  y += l.index(i) * s[len(l) - 1]
  l.remove(i)
  
print((abs(x-y)))
# Original solution
from itertools import permutations
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

t = 0
x = 0
y = 0
for i in permutations(list(range(1,n+1)),n):
  t += 1
  if i == p: x = t
  if i == q: y = t

print((abs(x-y)))

# Optimized solution
from itertools import permutations
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

nums = list(permutations(list(range(1,n+1)), n))
print((abs(nums.index(q) - nums.index(p))))
# Original solution
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

import itertools

per = list(itertools.permutations(list(range(1, N+1))))

p = -1
q = -1
for i in range(len(per)):
    if P == list(per[i]):
        p = i
    if Q == list(per[i]):
        q = i

print((abs(p - q)))

# Optimized solution
N = int(eval(input()))

P = list(map(int, input().split()))
Q = list(map(int, input().split()))
    
import itertools

per = list(itertools.permutations(list(range(1, N+1))))

p = 0
q = 0
for i in range(len(per)):
    if P == list(per[i]):
        p = i
    if Q == list(per[i]):
        q = i

print((abs(p - q)))
# Original solution
import itertools
n = int(eval(input()))
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))

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

print((abs(l.index(a) - l.index(b))))





# Optimized solution
import itertools
n = int(eval(input()))
a = tuple([int(i) for i in input().split()])
b = tuple([int(i) for i in input().split()])

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

print((abs(l.index(a) - l.index(b))))
# Original solution
from collections import deque

N = int(eval(input()))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

st = deque([[i] for i in range(N, 0, -1)])
ord = 0
a = 0
b = 0
while(len(st) > 0):
    if a > 0 and b > 0: break
    now = st.pop()
    if len(now) == N:
        ord += 1
    if P == now:
        a = ord
    if Q == now:
        b = ord
    for i in range(N, 0, -1):
        if i in now: continue
        st.append(now + [i])

print((abs(b - a)))

# Optimized solution
from itertools import permutations

N = int(eval(input()))
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())

perm = list(permutations(list(range(1, N + 1)), N))
i = 0
a = 0; b = 0;
for i in range(len(perm)):
    if a > 0 and b > 0: break
    if perm[i] == P:  a = i + 1
    if perm[i] == Q:  b = i + 1

print((abs(b - a)))
# Original solution
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

import itertools
X = list(itertools.permutations(list(range(1,N+1)),N))
for i in range(len(X)):
  x = X[i]
  if P == x:
    a = i 
  if Q == x:
    b = i

print((abs(a-b)))

# Optimized solution
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

import itertools
X = list(itertools.permutations(list(range(1,N+1)),N))

print((abs(X.index(Q)-X.index(P))))
# Original solution
def dfs(res, chk):
    if len(res) == n:
        global idx
        global pi
        global qi
        idx += 1
        pb = all(res[i]+1 == p for i, p in enumerate(P))
        qb = all(res[i]+1 == q for i, q in enumerate(Q))
        if pb:
            pi = idx
        if qb:
            qi = idx
        return
    
    for i in range(n):
        if chk & (1 << i):
            continue
        else:
            dfs(res + [i], chk^(1 << i))

n = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
idx = 0
pi = qi = 0
perm = []
dfs([], 0)
print((abs(pi-qi)))

# Optimized solution
# AtCoder Beginner Contest 150
# C - Count Order
# https://atcoder.jp/contests/abc150/tasks/abc150_c

from itertools import permutations

N = int(eval(input()))
P = tuple(i-1 for i in map(int, input().split()))
Q = tuple(i-1 for i in map(int, input().split()))

F = list(permutations(list(range(N))))

print((abs(F.index(P) - F.index(Q))))
# Original solution
n=int(eval(input()))
p=list(map(int,input().split()))
q=list(map(int,input().split()))
from itertools import permutations
for i,j in enumerate(permutations([i+1 for i in range(n)],n)):
  if p==list(j):a=i
  if q==list(j):b=i
print((abs(a-b)))

# Optimized solution
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
from itertools import permutations
for i,x in enumerate(permutations([i+1 for i in range(n)],n)):
  if x==p:a=i
  if x==q:b=i
print((abs(a-b)))
# Original solution
n = int(eval(input()))
#a,b = map(int,input().split())
L1 = tuple(map(int,input().split()))
L2 = tuple(map(int,input().split()))
L = [x+1 for x in range(n)]

li = []
import itertools
for x in itertools.permutations(L,n):
    #print(x)
    li.append(x)
#print(li)
print((abs(li.index(L1)-li.index(L2))))

# Optimized solution
n = int(eval(input()))
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
l = tuple(x+1 for x in range(n))
#print(a,b,l)
c,d = 0,0
cnt = 0
from itertools import permutations
for k in permutations(l):
    #print(k)
    cnt += 1
    if a ==k:
        c = cnt
    if b ==k:
        d = cnt
print((abs(c-d)))
# Original solution
import sys
from itertools import permutations

n = int(sys.stdin.readline().rstrip())
p, q = [tuple(map(int, sys.stdin.readline().split())) for _ in range(2)]

def main():
    perms = list(permutations(list(range(1, n+1))))
    return abs(perms.index(p) - perms.index(q))

if __name__ == '__main__':
    ans = main()
    print(ans)

# Optimized solution
import sys

n = int(sys.stdin.readline().rstrip())
*p, = list(map(int, sys.stdin.readline().split()))
*q, = list(map(int, sys.stdin.readline().split()))

factorial = [1] * (n + 1)
for i in range(n): factorial[i+1] = factorial[i] * (i + 1)

def main():
    d = 0
    a = [0] * (n + 1); b = [0] * (n + 1)
    for i in range(n - 1):
        a[p[i]] = b[q[i]] = 1
        c1 = c2 = 0
        for j in range(1, p[i]): c1 += a[j]
        for j in range(1, q[i]): c2 += b[j]
        d += ((q[i] - c2) - (p[i] - c1)) * factorial[n-(i+1)]
    print((abs(d)))

if __name__ ==  '__main__':
    main()
# Original solution
import itertools

n = int(eval(input()))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
# 多分1~4で3!の順列になる
ls = list(itertools.permutations(list(range(1,n+1)))) #順列生成
# 絶対値absで辞書順のpからqをひく
print((abs(ls.index(p)-ls.index(q))))

# Optimized solution
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fac = [0] * 8
# 1番目の桁
fac[1] = 1
for i in range(2, N):
    # 最上位の桁が7の場合7!の6周目終わりが30240番目が先頭になる。
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))
# l=3の時fac[2] 2!の2周目終わった時点え次のスタートが5になる
# 5の1番目のルートとなる
for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[i])

print((abs(a - b)))
# Original solution
from itertools import permutations

N = int(eval(input()))
P = "".join(list(input().split()))
Q = "".join(list(input().split()))
#print("P:", P)

A = ""
for i in range(1, N + 1):
  A += str(i)
A = list(permutations(A, N))
A.sort()
#print("A:", A)

B = []
for i in range(len(A)):
  B.append("".join(A[i]))

for i in range(len(B)):
  if P == B[i]:
    a = i
  if Q == B[i]:
    b = i
    
print((abs(a-b)))

# Optimized solution
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

A = list(permutations(list(range(1, N + 1))))
#print("A:", A)

print((abs((A.index(P) - A.index(Q)))))
# Original solution
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

A = list(permutations(list(range(1, N + 1))))
#print("A:", A)

print((abs((A.index(P) - A.index(Q)))))

# Optimized solution
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

D = list(permutations(list(range(1, N + 1))))
print((abs(D.index(P) - D.index(Q))))
# Original solution
from itertools import permutations

N = int(eval(input()))
Ps = list(map(int, input().split()))
Qs = list(map(int, input().split()))

for i, Rs in enumerate(permutations(list(range(1, N+1))), 1):
    Rs = list(Rs)
    if Ps == Rs:
        a = i
    if Qs == Rs:
        b = i

print((abs(a-b)))


# Optimized solution
from itertools import permutations

N = int(eval(input()))
Ps = tuple(map(int, input().split()))
Qs = tuple(map(int, input().split()))

noP, noQ = 0, 0
for no, Rs in enumerate(permutations(list(range(1, N+1)), r=N), start=1):
    if Ps == Rs:
        noP = no
    if Qs == Rs:
        noQ = no

print((abs(noP-noQ)))
# Original solution
import itertools
n = int(eval(input()))
p = input().replace(' ', '')
q = input().replace(' ', '')

for i, num in enumerate(itertools.permutations(list(range(1,n+1)))):
    num = ''.join(map(str, list(num)))
    if num == p:
        break
for j, num in enumerate(itertools.permutations(list(range(1,n+1)))):
    num = ''.join(map(str, list(num)))
    if num == q:
        break

print((abs(i-j)))

# Optimized solution
import itertools
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

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


def f(val):
    for i, x in enumerate(N):
        if x == val:
            return i+1

print((abs(f(p) - f(q))))
# Original solution
import itertools
 
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
# Nが10程度で小さい
# 階乗の全探索をしても間に合う
# Pythonならitertoolsが使える
# permutations（関数）にlistを渡すと、順列を生成！
l = [i for i in range(1, N+1)]
permutations_l = list(itertools.permutations(l))

a, b = 0, 0
for i, R in enumerate(permutations_l):
  if R == P: a = i
for i, R in enumerate(permutations_l):
  if R == Q: b = i

print((abs(a-b)))

# Optimized solution
import itertools
 
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
# Nが10程度で小さい
# 階乗の全探索をしても間に合う
# Pythonならitertoolsで順列を生成できる！
l = [i for i in range(1, N+1)]
permutations_l = list(itertools.permutations(l))
 
a, b = 0, 0
for i, R in enumerate(permutations_l):
  if R == P: a = i
  if R == Q: b = i

print((abs(a-b)))
# Original solution
import sys

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))


last = [n-1 for i in range(n)]

a,b = 0,0

l = [i+1 for i in range(n)]
if l == p:
	a = 1
if l == q:
	b = 1

if a == b == 1:
	print((0))
	sys.exit()
cnt = 1
while True:
	cnt += 1
	for i in range(n-1,-1,-1):
		if l[i] < l[i-1]:
			continue
		else:
			obj = l[i-1]
			tmp = sorted(l[i-1:])
			switch = tmp.pop(tmp.index(obj)+1)
			l = l[:i-1] + [switch] + sorted(tmp)

			#print(l)

			break
	
	if l == p:
		a = cnt
	if l == q:
		b = cnt

	if a != 0 and b != 0:
		break

print((abs(a-b)))



# Optimized solution
import itertools

n = int(eval(input()))
p = "".join(list(map(str, input().split())))
q = "".join(list(map(str, input().split())))

l = "".join([str(i+1) for i in range(n)])

cnt = 0
a,b = 0,0
for x in itertools.permutations(l):
	y = "".join(x)
	cnt += 1
	if y == p:
		a = cnt
	if y == q:
		b = cnt

	if a != 0 and b != 0:
		break

print((abs(a-b)))