CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    n=int(eval(input()))
    p=list(map(int,input().split()))
    q=list(map(int,input().split()))
    fact = [1]
    for i in range(1,9):
        fact.append(i*fact[i-1])
    tmp = []
    for i in range(n):
        tmp.append(i+1)
    i=0
    ansp = caldict(fact,i,p,tmp)
    tmp = []
    for i in range(n):
        tmp.append(i+1)
    i=0
    ansq = caldict(fact,i,q,tmp)

    print((abs(ansp-ansq)))
def caldict(f,i,p,tmp):
    if i < len(p)-1:
        nowi = tmp.index(p[i])
        now = tmp.pop(nowi)
        i+=1
        return nowi * f[len(tmp)] + caldict(f,i,p,tmp)
    else:
        return 1


if __name__=='__main__':
    main()


# Optimized solution
import itertools
def main():
    n=int(eval(input()))
    p=list(map(int,input().split()))
    q=list(map(int,input().split()))

    perm = list(itertools.permutations(list(range(1,n+1))) )
    ansp = perm.index(tuple(p))
    # qq = list(itertools.permutations(q) )
    ansq = perm.index(tuple(q))

    print((abs(ansp-ansq)))

if __name__=='__main__':
    main()
# Original solution
import itertools
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
A = [i for i in range(1,N+1)]
count = 1
for i in itertools.permutations(A):
    flg = True
    for k in range(N):
        if i[k] != P[k]:
            flg = False
            break
    if flg:
        a = count
    flg = True
    for k in range(N):
        if i[k] != Q[k]:
            flg = False
            break
    if flg:
        b = count
    count += 1
print((abs(a-b)))

# Optimized solution
import itertools
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = [i for i in range(1,N+1)]
cnt = 0
ans_a, ans_b = 0, 0
for target in itertools.permutations(a):
    cnt += 1
    if list(target) == P:
        ans_a = cnt
    if list(target) == Q:
        ans_b = cnt
print((abs(ans_a-ans_b)))
# Original solution
import itertools
n = int(eval(input()))
a = tuple(i for i in range(1, n+1))
x = tuple(map(int, input().split()))
y = tuple(map(int, input().split()))

for i, z in enumerate(tuple(itertools.permutations(a))):
    if z == x:
        x_count = i
    if z == y:
        y_count = i
print((abs(x_count - y_count)))

# Optimized solution
from itertools import permutations
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
comb = sorted(list(permutations([i for i in range(1, N+1)], N)))
print((abs((comb.index(P) + 1) - (comb.index(Q) + 1))))
# Original solution
import itertools

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

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

Pi, Qi = -1, -1
for i in range(len(LUT)):
    if LUT[i] == P:
        Pi = i
    if LUT[i] == Q:
        Qi = i

print((abs(Qi - Pi)))


# Optimized solution
from itertools import *

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

LUT = list(permutations(list(range(1, N + 1)))).index
print((abs(LUT(P) - LUT(Q))))
# Original solution
import itertools
N=int(eval(input()))
P,Q=[tuple(map(int,input().split())) for i in range(2)]
dictionary=sorted(itertools.permutations(P,N))
print((abs(dictionary.index(P)-dictionary.index(Q))))

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

for i in itertools.permutations(list(range(1,n+1))):
  count+=1
  if i == p:
    a=count
  if i==q:
    b=count
print((abs((count-a)-(count-b))))
# Original solution
import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = b = cnt = 0
R = []

for i in range(1,N+1):
  R.append(i)

for permutation in itertools.permutations(R):
  cnt += 1
  if permutation == P:
    a = cnt
  if permutation == Q:
    b = cnt

ans = abs(a-b)
print(ans)


# Optimized solution
import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
L = list(itertools.permutations(list(range(1, N+1))))

ans = abs(L.index(P)-L.index(Q))
print(ans)
# Original solution
import itertools
N = int(eval(input()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
seq = [x for x in range(1,N+1)]
L = list(itertools.permutations(seq))
for i in range(len(L)):
    X = list(L[i])
    if X == P:
        a = i + 1
    if X == Q:
        b = i + 1
ans = abs(a-b)
print(ans)

# Optimized solution
import itertools

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

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

N = int(eval(input()))

P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
res = 0
loop = 0

d = defaultdict(int)

for perm in permutations(list(range(1,N+1))):
    d[perm] = loop
    loop += 1

res = abs(d[P] - d[Q])

print(res)

# Optimized solution
from itertools import *
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
dic = dict()
i = 0
for a in permutations(list(range(1,N+1)),N):
    dic[a] = i
    i += 1
print((abs(dic[P]-dic[Q])))
# Original solution
import itertools

N = int(eval(input()))
P = int(input().replace(' ', ''))
Q = int(input().replace(' ', ''))
s = [i for i in range(1, N + 1)]
l = list(itertools.permutations(s))
j = []
for i in range(len(l)):
  t = list(map(str,l[i]))
  j.append(int(''.join(t)))

nl = sorted(j) 
a = 0
b = 0

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

# Optimized solution
import itertools

N = int(eval(input()))
P = int(input().replace(' ', ''))
Q = int(input().replace(' ', ''))
l = [i for i in range(1, N+1)]
pl = itertools.permutations(l)
lis = []
for oc in pl:
  lis.append(int(''.join(map(str, list(oc)))))
lis.sort()
a = lis.index(P)
b = lis.index(Q)

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

import itertools
N = int(eval(input()))
a = list(itertools.permutations(list(range(1, N+1))))

s = []
for i in a:
  ans = ""
  for j in i:
    ans+=str(j)
  s.append(ans)

P = "".join(list(input().split()))
Q = "".join(list(input().split()))
ans = abs((s.index(P)+1)-(s.index(Q)+1))
print(ans)


# Optimized solution
# abc150_c.py
'''
全パターン作ってソートで順番みる
'''
import itertools
N = int(eval(input()))
P = tuple(list(map(int,input().split())))
Q = tuple(list(map(int,input().split())))

l = [i+1 for i in range(N)]
target =  itertools.permutations(l)
cnt=0
pcnt = 0
qcnt = 0
for i in target:
    cnt += 1
    if i == P:
        pcnt=cnt
    if i == Q:
        qcnt=cnt

print((abs(pcnt-qcnt)))
# Original solution
import math

def number(plist):
    a = 1
    memo = []
    while len(plist) > 0:
        p = plist.pop(0)
        memo.append(p)
        num = len(set(range(1, p)) - set(memo))
        a += math.factorial(len(plist)) * num
    return a

n = int(eval(input()))
plist = list(map(int, input().split()))
qlist = list(map(int, input().split()))

a = number(plist)
b = number(qlist)
print((int(math.fabs(a - b))))

# Optimized solution
import math
from itertools import permutations
def generate_permutations(n):
    perms = permutations(list(range(1, n+1)), n)
    return [perm for perm in perms]
 
n = int(eval(input()))
ptuple = tuple(map(int, input().split()))
qtuple = tuple(map(int, input().split()))
 
perms = generate_permutations(n)
a = perms.index(ptuple)
b = perms.index(qtuple)
print((int(math.fabs(a - b))))
# Original solution
from itertools import permutations

N = int(eval(input()))
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

P = []
for perm in permutations(list(range(1, N + 1)), r=N):
    P.append(tuple(perm))

P.sort()
print((abs(P.index(A) - P.index(B))))

# Optimized solution
from itertools import permutations

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

X = [tuple(p) for p in permutations(list(range(1, N + 1)), r=N)]
X.sort()

print((abs(X.index(P) - X.index(Q))))
# Original solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools

# 標準入力を取得
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

# 求解処理
p = 0
q = 0
order = 1
for t in itertools.permutations(list(range(1, N + 1)), N):
    if t == P:
        p = order
    if t == Q:
        q = order
    order += 1

ans = abs(p - q)

# 結果出力
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    # 標準入力を取得
    N = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))

    return N, P, Q


def main(N: int, P: tuple, Q: tuple) -> None:
    """
    メイン処理.

    Args:\n
        N (int): 数列の大きさ
        P (tuple): 順列
        Q (tuple): 順列
    """
    # 求解処理
    p = 0
    q = 0
    order = 1
    for t in itertools.permutations(list(range(1, N + 1)), N):
        if t == P:
            p = order
        if t == Q:
            q = order
        order += 1

    ans = abs(p - q)

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, P, Q = get_input()

    # メイン処理
    main(N, P, Q)
# Original solution
import itertools, sys

N = int(eval(input()))
N_list = [i for i in range(1, N+1)]

A = list(map(int, input().split()))
B = list(map(int, input().split()))

if A == B:
    print('0')
    sys.exit()
for v, i  in enumerate(list(itertools.permutations(N_list))):
    i = list(i)
    if A == i:
        a = v
    elif B == i:
        b = v

print((abs(a-b)))


# Optimized solution
import itertools

N = int(eval(input()))
N_list = [i for i in range(1, N+1)]

A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

N_pattern = list(itertools.permutations(N_list))

print((abs(N_pattern.index(A) - N_pattern.index(B))))
# Original solution
import itertools

N = int(eval(input()))
N_list = [i for i in range(1, N+1)]

A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

N_pattern = list(itertools.permutations(N_list))

print((abs(N_pattern.index(A) - N_pattern.index(B))))


# Optimized solution
import itertools
n = int(eval(input()))
ans = list(itertools.permutations(list(range(1,n+1))))

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

a = ans.index(P)
b = ans.index(Q)
print((abs(a-b)))
# Original solution
import itertools

n = int(eval(input()))

order = list(itertools.permutations([int(x)for x in range(1,n+1)]))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

for i in range(len(order)):
    if P == list(order[i]):
        P_num = i
    if Q == list(order[i]):
        Q_num = i
        
print((abs(P_num-Q_num)))

# Optimized solution
import sys
import itertools

input = sys.stdin.readline

def slove():
    n = int(eval(input()))
    P = tuple(map(int,input().split()))
    Q = tuple(map(int,input().split()))
    """
    print(P)
    print(Q)
    """
    count = 0
    for item in itertools.permutations(list(range(1,1+n))):
        if P == item:
            a = count
        if Q == item:
            b = count
        count += 1
    print((abs(a-b)))


if __name__ == "__main__":
    slove()
# Original solution
import itertools
N=int(eval(input()))
candi=[str(i) for i in range(1,N+1)]
P="".join(input().split())
Q="".join(input().split())
count=0
#候補列挙
for j in itertools.permutations(candi,N):
  k="".join(list(j))
  count+=1
  if k==P:
    a=count
  if k==Q:
    b=count
print((abs(a-b)))

# Optimized solution
#Favorite Answers
from itertools import permutations
n=int(eval(input()))
p=tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
l = list(permutations(list(range(1,n+1))))
print((abs(l.index(p)-l.index(q))))
# Original solution
import itertools

n = int(eval(input()))
a = [ str(x) for x in range(1,n+1)]
jun = list(itertools.permutations(a))
p = list(map(str, input().split()))
q = list(map(str, input().split()))
p = ''.join(p)
q = ''.join(q)
a = []

for i in jun:
    i = list(i)
    a.append(''.join(i))
x = a.index(p)
y = a.index(q)
print((abs(x-y)))





# Optimized solution
import itertools
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
t,h = 0,0
a = list(itertools.permutations(list(range(1,n+1))))
for i,c in enumerate(a):
    if p == c:
        t = i
    if q == c:
        h = i
print((abs(t-h)))
# Original solution
import itertools

N = int(eval(input()))
P = [int(x) for x in input().split()]
P = tuple(P)
Q = [int(x) for x in input().split()]
Q = tuple(Q)
N_ = list(range(1, N+1))

R = list(itertools.permutations(N_))

Pflg = Qflg = False
Pcnt = Qcnt = -1
for i, r in enumerate(R):
    if P==r:
        Pflg = True
        Pcnt = i
    if Q==r:
        Qflg = True
        Qcnt = i
    if Pflg and Qflg:
        break

print((abs(Pcnt - Qcnt)))

# Optimized solution
import itertools

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

pmt = list(itertools.permutations(list(range(1, N+1))))
pmtP = pmt.index(P)
pmtQ = pmt.index(Q)
print((abs(pmtP - pmtQ)))
# Original 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())

ps = list(permutations(list(range(1, N + 1))))
ps.index(P)

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


# 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())

ps = permutations(list(range(1, N + 1)))

try:
    x, y = [i for i, t in enumerate(ps) if t == P or t == Q]
except ValueError:
    print((0))
else:
    print((abs(x - y)))
# Original solution
import itertools
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
val_list = [False for _ in range(pow(10, n))]


def make_val(suretsu):
    val = 0
    for index in range(n):
        val += suretsu[n - index - 1] * pow(10, index)
    return val

p_val = make_val(p)
q_val = make_val(q)
min_val = min(p_val, q_val)
max_val = max(p_val, q_val)

count = 0
counting = False
if min_val == max_val:
    print((0))
else:
    for suretsu in itertools.permutations(list(range(1, n+1))):
        val = make_val(suretsu)
        if val == min_val:
            counting = True
        if val == max_val:
            break
        if counting:
            count += 1


    print(count)

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

def make_val(suretsu):
    val = 0
    for index in range(n):
        val += suretsu[n - index - 1] * pow(10, index)
    return val

p_val = make_val(p)
q_val = make_val(q)
min_val = min(p_val, q_val)
max_val = max(p_val, q_val)

count = 0
counting = False
p_rank = 0
q_rank = 0
for suretsu in itertools.permutations(list(range(1, n+1))):
    val = make_val(suretsu)
    if val < p_val:
        p_rank += 1
    if val < q_val:
        q_rank += 1
    p_rank += 1
    q_rank += 1

print((abs(p_rank-q_rank)))
# Original solution
import math
import itertools
n=int(eval(input()))

a=list(map(str,input().split()))
b=list(map(str,input().split()))

p=str(math.factorial(n))

a_list = [''.join(v) for v in itertools.permutations(a)]
a_int = [int(i) for i in a_list]
a_int.sort()

a_=[int(i) for i in a]
b_=[int(j) for j in b]
x="".join(a)
y="".join(b)

p_=a_int.index(int(x))+1
q_=a_int.index(int(y))+1

sum=abs(p_-q_)
print(sum)


# Optimized solution
import math
import itertools
n=int(eval(input()))

a=list(map(str,input().split()))
b=list(map(str,input().split()))

a_str = [''.join(v) for v in itertools.permutations(a)]
a_int = [int(i) for i in a_str]
a_int.sort()

x="".join(a)
y="".join(b)

p=a_int.index(int(x))+1
q=a_int.index(int(y))+1

print((abs(p-q)))
# Original solution
import itertools

N = int(eval(input()))
T = list(range(1,N+1))
S = list(itertools.permutations(T, N))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

for i in range(len(S)):
    if tuple(P) == S[i]:
        A = i
    if tuple(Q) == S[i]:
        B = i
print((abs(A-B)))

# Optimized solution
import itertools

N = int(eval(input()))
T = list(range(1,N+1))
S = list(itertools.permutations(T, N))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

A = S.index(tuple(P))
B = S.index(tuple(Q))
print((abs(A-B)))
# Original solution
import itertools

N = int(eval(input()))
PER = []
for i in range(N):
    PER.append(i+1)

P = list(map(int,input().split()))
Q = list(map(int,input().split()))
a = 0
b = 0
tmp = 1

for x in itertools.permutations(PER):
     
    flag_a = 0
    flag_b = 0
 
    for i in range(N):
        if x[i] == P[i]:
            flag_a += 1   
        if x[i] == Q[i]:
            flag_b += 1
    if flag_b == N:
        b = tmp
    if flag_a == N:
        a = tmp
    tmp += 1

ans = a - b
if ans < 0:
    ans = - ans

print(ans)
        

# Optimized solution
from itertools import permutations

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

p = list(permutations(list(range(1,N+1))))

print((abs(p.index(P)-p.index(Q))))
# Original solution
import itertools

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

index=0
for i in itertools.permutations(list(range(1,N+1)), N):   
    if P==list(i):
        a=index
    if Q==list(i):
        b=index
    index+=1
print((abs(b-a)))

# Optimized solution
import sys
input = sys.stdin.readline

def I(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    P=LI()
    Q=LI()
    
    
    import itertools
    
    cnt=0
    p=0
    q=0
    for ite in itertools.permutations(list(range(1,N+1))):
        if P==list(ite):
            p=cnt
        if Q==list(ite):
            q=cnt    
        cnt+=1
        
    print((abs(p-q)))
        
        
main()
# Original solution
def abc150_c_count_order():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))

    def solve(items, current):
        if items == []:
            permutations.append(current)
            return
        for item in items:
            next = current + [item]
            new_items = [i for i in items if i != item]
            solve(new_items, next)

    permutations = []
    items = list(range(1, N+1))
    current = []
    solve(items, current)

    a = permutations.index(P)
    b = permutations.index(Q)
    print((abs(a - b)))


##########
if __name__ == "__main__":
    abc150_c_count_order()


# Optimized solution
def abc150_c_count_order_2():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    factorial = [1]
    for i in range(1, N+1):
        factorial.append(factorial[i-1] * i)

    def find_index(permutation, lst):
        indices = []
        for p in permutation:
            i = lst.index(p)
            indices.append(i)
            lst.pop(i)
        # print(indices)
        idx = 0
        for i in range(N):
            idx += factorial[N - i - 1] * (indices[i])
        # print(idx)
        return idx

    a = find_index(P, list(range(1, N+1)))
    b = find_index(Q, list(range(1, N+1)))
    print((abs(a-b)))


##########
if __name__ == "__main__":
    abc150_c_count_order_2()
# Original solution
N=int(eval(input()))
P=list(input().split())
Q=list(input().split())
P=int(''.join(P))
Q=int(''.join(Q))
n=[i for i in range(1,N+1)]
A=[]
def dps(n,A,me):
    if len(n)==0:
        A.append(me)
        return
    else:
        for i in range(0, len(n)):
            t=n.pop(i)
            dps(n, A, me*10+t)
            n.insert(i, t)
dps(n,A,0)
print((abs(A.index(P)-A.index(Q))))

# Optimized solution
N=int(eval(input()))
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
import itertools
l=list(itertools.permutations([i for i in range(1, N+1)]))
print((abs(l.index(P)-l.index(Q))))
# Original solution
import itertools

N = int(eval(input()))
P_list = list(map(int, input().split()))
Q_list = list(map(int, input().split()))


for i, v in enumerate(itertools.permutations(list(range(1,N+1)))):
    if list(v) == P_list:
        a = i + 1
    if list(v) == Q_list:
        b = i + 1

print((abs(a-b)))

# Optimized solution
import itertools

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

count = 1
for p in itertools.permutations(list(range(1, N+1))):

    if p == P:
        count_P = count
    if p == Q:
        count_Q = count

    count += 1

ans = abs(count_P - count_Q)
print(ans)
# Original solution
# -*- coding: utf-8 -*-
from itertools import permutations

N = int(input().strip())
pat_P = [ "".join( input().rstrip().split() )][0]
pat_Q = [ "".join( input().rstrip().split() )][0]
#-----

perm_list = [ "".join( map(str, pat) ) for pat in permutations(list(range(1,N+1)))]
perm_list.sort()

a = perm_list.index(pat_P) + 1
b = perm_list.index(pat_Q) + 1
print((abs(a-b)))


# Optimized solution
# -*- coding: utf-8 -*-
from itertools import permutations

N = int(input().strip())
pat_P = tuple(map(int, input().rstrip().split()))
pat_Q = tuple(map(int, input().rstrip().split()))
#-----

perm_list = [ pat for pat in permutations(list(range(1,N+1)))]

a = perm_list.index(pat_P) + 1
b = perm_list.index(pat_Q) + 1
print((abs(a-b)))
# Original solution
import itertools

n = int(eval(input()))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
ls = list(itertools.permutations(list(range(1,n+1))))
print((abs(ls.index(p)-ls.index(q))))

# Optimized solution
import itertools

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

dict=list(itertools.permutations(list(range(1,n+1))))
print((abs(dict.index(p)-dict.index(q))))
# Original solution
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)))

# Optimized solution
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()
# Original solution
from collections import deque
n = int(eval(input()))
p_que = deque(list(map(int, input().split())))
q_que = deque(list(map(int, input().split())))

que = deque(i for i in range(1, n+1))
cnt = 0
a = 0
b = 0

def dfs(d, q):
  # print('-'*10, q)
  # sorted(q)
  for i in list(q):
    # print(i, d, q)
    d.append(i)
    q.remove(i)
    # print('>', d, q)
    if q == deque([]):
      global cnt
      cnt += 1
      # print('>>>', cnt, d, q)
      if d == p_que:
        global a
        a = cnt
      if d == q_que:
        global b
        b = cnt
      # print('return')
      d.remove(i)
      q.appendleft(i)
      return
    # print('tansaku')
    dfs(d, q)
    # print('>returned')
    # print('---', i, d, q)
    d.remove(i)
    if i > q[0]:
      q.append(i)
    else:
      q.appendleft(i)
    q = deque(sorted(q))

dfs(deque(), que)
# print(a, b)
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()))

n_list = [x for x in range(1, n+1)]
# print(list(permutations(n_list, n)))
a = b = 0
for n, i in enumerate(permutations(n_list, n)):
  # print(n, i)
  if i == P:
    a = n
  if i == Q:
    b = n
    
print((abs(a-b)))