We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).
There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

-----Notes-----
For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

-----Constraints-----
 - 2 \leq N \leq 8
 - P and Q are permutations of size N.

-----Input-----
Input is given from Standard Input in the following format:
N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

-----Output-----
Print |a - b|.

-----Sample Input-----
3
1 3 2
3 1 2

-----Sample Output-----
3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Difficulty: introductory

Starter code:

Solutions:
import itertools
import bisect

N = int(input())
Nums = [str(n) for n in range(1, N+1)]

p = int(''.join([n for n in input().split()]))
q = int(''.join([n for n in input().split()]))

per = itertools.permutations(Nums)

numlist = sorted([int(''.join(list(s))) for s in per])

a = bisect.bisect_left(numlist, p)
b = bisect.bisect_left(numlist, q)

print(abs(a-b))

import math

def calc(lst):
    ans = 0
    aft = []
    for i in range(N):
        kurai = lst[i]
        aft.append(kurai)

        for j in aft:
            if j < lst[i]:
                kurai -= 1
        
            
        ans = ans + (kurai - 1)*math.factorial(N-(i+1))

    return ans + 1

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

ans = abs(calc(P) - calc(Q))
print(ans)

import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ x for x in itertools.permutations(range(1, N + 1)) ]
a = [ key for key, val in enumerate(ls) if P == val ][0] + 1
b = [ key for key, val in enumerate(ls) if Q == val][0] + 1
print(abs(a - b))

import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ i for i in itertools.permutations(range(1, N + 1)) ]
a = [ idx for idx, val in enumerate(ls) if val == P ][0] + 1
b = [ idx for idx, val in enumerate(ls) if val == Q ][0] + 1
print(abs(a - b))

import itertools

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

import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
P = tuple(itertools.permutations(range(1, n+1)))

print(abs(P.index(p) - P.index(q)))

import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
S = [i for i in range(1, N+1)]

perm = list(itertools.permutations(S))
ans = []
for i, p in enumerate(perm):
    if list(p) == P or list(p) == Q:
        ans.append(i+1)

if len(ans) > 1:
    print(abs(ans[0] - ans[1]))
else:
    print(0)

import itertools
def calc_cnt(nums,n):
    ans=1
    data=[str(i) for i in range(1,n+1)]
    for x in itertools.permutations(data):
        if " ".join(x)==nums:
            return ans
        ans+=1
    return ans

def main():
    n=int(input())
    p=input()
    q=input()
    p_cnt=calc_cnt(p,n)
    q_cnt=calc_cnt(q,n)
    ans=abs(p_cnt-q_cnt)
    print(ans)
def __starting_point():
    main()
__starting_point()

import itertools
n = int(input())
N = list(i for i in range(1,n+1))
L = itertools.permutations(N,n)
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
cnt, s, e = 0, 0, 0

for l in L:
  cnt += 1
  if a == l:
    s = cnt
  if b == l:
    e = cnt
print((abs(s-e)))



import itertools
import copy
n = int(input())
P = list(map(int, input().split()))
Q = tuple(list(map(int, input().split())))
P_ = copy.copy(P)
P_.sort()
p_list = list(itertools.permutations(P_))
P = tuple(P)

a = p_list.index(P)
b = p_list.index(Q)

print(abs(a-b))

from itertools import permutations

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

count = 0
for i in permutations(sorted(P)):
    if(list(i) == P):
        a = count
    if(list(i) == Q):
        b = count
    count += 1
print((abs(a-b)))


import itertools
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(map(int, input().split()))
def i_row(N): return [int(input()) for _ in range(N)]
def i_row_list(N): return [list(map(int, input().split())) for _ in range(N)]

n=i_input()
pp=tuple(i_list())
qq=tuple(i_list())
a,b=-1,-1
for i, rr in enumerate(itertools.permutations(range(1, n + 1))):
    if a==-1 and pp==rr:
        a=i
    if b==-1 and qq==rr:
        b=i
    if a!=-1 and b!=-1:
        break
print(abs(a-b))

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

l = [i for i in range(1,n+1)]
p = itertools.permutations(l, n)
i = 0

for v in itertools.permutations(l, n):
    i += 1
    if(v == P) :
        a = i
    if(v == Q) :
        b = i
print(abs(a-b))

import itertools

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
a=[i+1 for i in range(N)]
x=0
y=0
i=0
for b in itertools.permutations(a):
    if list(b)==P:
        x=i
    if list(b)==Q:
        y=i
    i+=1
print((abs(x-y)))


import itertools

n = int(input())

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

lst = list(itertools.permutations(list(range(1, n + 1))))
print(abs(lst.index(p) - lst.index(q))) 

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

l = [i+1 for i in range(N)]
for i, tpl in enumerate(itertools.permutations(l, N)):
    if tpl == P:
        a = i
    if tpl == Q:
        b = i
print(abs(a-b))

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

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


a = 0
b = 0

for x in pattern:
  new_x = list(x)
  if new_x == p:
    a = pattern.index(x)
  if new_x == q:
    b = pattern.index(x)


print((abs(a-b)))


import itertools

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

a=[]
for i in range(1,n+1):
        a.append(i)
b=[]
for v in itertools.permutations(a, n):
        b.append(v)

x=0
for j in b:
        if list(j)==p:
              x=b.index(j)
y=0
for k in b:
        if list(k)==q:
                y=b.index(k)

print(abs(x-y))

N,P,Q=open(0);*P,=map(int,P.split());*Q,=map(int,Q.split())
f=lambda i:i*f(i-1)if i else 1
g=lambda l:(l[0]-1)*f(len(l)-1)+g([e-1if e>l[0]else e for e in l[1:]])if l else 0
print(abs(g(P)-g(Q)))

import sys
read = sys.stdin.read
#readlines = sys.stdin.readlines
from itertools import permutations
def main():
    n = int(input())
    p = "".join(input().split())
    q = "".join(input().split())

    chars = [str(i) for i in range(1, n + 1)]
    per = list(permutations(chars, n))
    per_str = ["".join(pere) for pere in per]
    per_str.sort()
    p_pos = per_str.index(p)
    q_pos = per_str.index(q)
    ans = abs(p_pos - q_pos)
    print(ans)

def __starting_point():
    main()

__starting_point()

import itertools

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

p = list(itertools.permutations(range(1,N+1)))
a = p.index(P)
b = p.index(Q)
print(abs(a-b))

import itertools
n = int(input())
p = [int(i)-1 for i in input().split()]
q = [int(i)-1 for i in input().split()]
m = sorted(p)
a = 0
b = 0
cnt = 1
#print(p,q)
for i in itertools.permutations(m, n):
  #print(list(i))
  if list(i) == p:
    a += cnt
  if list(i) == q:
    b += cnt
  if a!=0 and b!=0:
    break
  else:
    cnt += 1
    
print(abs(a-b))

import itertools
import math

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
p_no, q_no = 0, 0

per = [c+1 for c in range(n)]
per_list = list(itertools.permutations(per))

for i in range(math.factorial(n)):
    if p == per_list[i]:
        p_no = i
    if q == per_list[i]:
        q_no = i

print(abs(p_no - q_no))

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

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

a = 0
b = 0

for i in range(len(l)):
    s = [i for i in l[i]]

    if s == p:
        a = i 
    if s == q:
        b = i 
        
print(abs(a-b))

from itertools import permutations
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
P=sorted(list(permutations(range(1,n+1),n)))
print(abs(P.index(p)-P.index(q)))

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

remain_p = sorted(p)
remain_q = sorted(q)
a = 0
b = 0
for i in range(n):
	a += math.factorial(n - (i + 1)) * (remain_p.index(p[i]) + 1) - 1
	b += math.factorial(n - (i + 1)) * (remain_q.index(q[i]) + 1) - 1
	remain_p.remove(p[i])
	remain_q.remove(q[i])

ans = a - b
if ans >= 0:
	print(ans)
else:
	print((-ans))


from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

for i, p in enumerate(permutations(list(range(1, N+1)))):
    if p == P:
        a = i
    if p == Q:
        b = i
print((abs(a-b)))


import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = [ i for i in range(1, N + 1)]
K = list(itertools.permutations(L))
K.sort()
p, q = 0, 0

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

print(abs(p - q))

import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
a = list(itertools.permutations(list(range(1,n+1))))
pp,qq = 0,0
for i,j in enumerate(a):
  if p == j:
    pp = i
  if q == j:
    qq = i
print(abs(pp-qq))

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

def kaijo(x):
  ans = 1
  for i in range(x):
    ans = ans * (i + 1)
  return ans

def junban(dic):
  order = 1
  used = []
  for i in range(N):
    minus = 1
    
    for j in used:
      if dic[i] > j:
        minus += 1
    order += (dic[i] - minus) * kaijo(N - i - 1)
    used.append(dic[i])
  
  return(order)

Porder = junban(P)
Qorder = junban(Q)

if Porder >= Qorder:
  print(Porder - Qorder)
else:
  print(Qorder - Porder)

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

import itertools
P_list = list(itertools.permutations(P, len(P)))
P_list.sort()
p=P_list.index(P)
q=P_list.index(Q)
print(abs(p-q))

#https://atcoder.jp/contests/abc150/tasks/abc150_c

from itertools import permutations

b= int(input())
c= []

d= list(map(int, input().split()))
e= list(map(int, input().split()))

for a in range(1, b+1):
    c.append(a)

c= list(map(str, range(1, b+1)))

#print(c)
d= ''.join(map(str, d))
e= ''.join(map(str, e))

f= list(map(''.join, permutations(c, b)))
#print(f)

D= f.index(d)
E= f.index(e)
if(D>E):
    print(D-E)
else:
    print(E-D)

import numpy as np
from itertools import permutations
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
permu = [i for i in range(1, n+1)]
permu = permutations(permu, n)

for i, s in enumerate(permu):
    if p == s:
        a = i+1
    if q == s:
        b = i+1

print((abs(a-b)))


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

queue = [[]]
lst = []

while queue:
  e = queue.pop(-1)
  if len(e) == n:
    lst.append(tuple(e))
    continue
  for i in range(1,n+1):
    if i not in e:
      queue.append(e+[i])

lst.sort()
print((abs(lst.index(P) - lst.index(Q))))




import math

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
a = 0
b = 0

for i in range(0,n,1):
    a += (p[i]-1)*math.factorial(n-i-1)
    b += (q[i]-1)*math.factorial(n-i-1)
    for  j in range(i+1,n,1):
        if p[j] > p[i]:
            p[j] -= 1
        if q[j] > q[i]:
            q[j] -= 1

print(abs(a-b))

import itertools

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

P = list(itertools.permutations(N_list))
P.sort()

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

Q_number = P.index(Q) + 1
O_number = P.index(O) + 1

print((abs(Q_number - O_number)))




import itertools

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

X = list(itertools.permutations(P))
X.sort()

a = X.index(P)
b = X.index(Q)


print(abs(a-b))

from itertools import permutations

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

dictionary = list(permutations(list(range(1, N+1))))
a = dictionary.index(P)
b = dictionary.index(Q)
print((abs(a-b)))


import math

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

def library_number(x,n):
  l = list(range(1,n+1))
  tmp = 0
  for i in range(n):
    tmp += math.factorial(n-1-i)*l.index(x[i])
    l.remove(x[i])
  return tmp

ans = abs(library_number(p,n) - library_number(q,n))
print(ans)

import itertools

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

p = list(itertools.permutations(range(1,N+1)))
a = p.index(P) + 1
b = p.index(Q) + 1
print(abs(a-b))

import itertools
n=int(int(input()))
a=[i+1 for i in range(n)]
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
c=0
d=0
e=0
for i in itertools.permutations(a,n):
    c+=1
    if p==i:
        d=c
    if q==i:
        e=c
print(abs(d-e))

import itertools

n = int(input())

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

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

ans = list(itertools.permutations(p))

ans.sort()

p = ans.index(p)

q = ans.index(q)

print(abs(p - q))

import itertools

N = int(input())
l = list(itertools.permutations(range(1,N+1)))

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

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

import itertools
N = int(input())
P = tuple([int(i) for i in input().split()])
Q = tuple([int(i) for i in input().split()])
perm = sorted(list(itertools.permutations(P)))
print(abs(perm.index(P)-perm.index(Q)))

import itertools
n = int(input())
s = ""
for i in range(n):
  s += str(i+1)
l = list(itertools.permutations(s, n))
p = tuple(input().split())
q = tuple(input().split())
a = l.index(p)
b = l.index(q)
print(abs(a-b))

# coding: utf-8

def check(A):
    tmp = 0
    first = A[0]
    A.pop(0)
    for i in A:
        if i < first:
            tmp += 1
    return tmp

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    con = [1, 1, 2, 6, 24, 120, 720, 5040, 40320]

    a, b = 0, 0

    for i in range(N, 0, -1):
        a += con[i - 1] * check(P)
        b += con[i - 1] * check(Q)

    print((abs(a - b)))

def __starting_point():
    main()

__starting_point()

from math import factorial as fac

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

def F(List):
  ans = 1
  L = len(List)
  nums = list(range(1,N+1))
  for i in range(L):
    ans += fac(L-i-1)*(nums.index(List[i]))
    nums.remove(List[i])
  return ans

print(abs(F(P)-F(Q)))

import itertools 
import math

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

l=[x for x in range(1,N+1)]
lst=list(itertools.permutations(l))

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

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

i = 1
a = 0
b = 0
for l in itertools.permutations(list(range(1, N+1))):
    if ''.join([str(n) for n in P]) == ''.join([str(n) for n in l]):
        a = i
    if ''.join([str(n) for n in Q]) == ''.join([str(n) for n in l]):
        b = i
    i += 1
print((abs(a-b)))


import itertools

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

l = [i+1 for i in range(n)]

perm_li = [v for v in itertools.permutations(l)]

print((abs(perm_li.index(p) - perm_li.index(q))))
           


from itertools import permutations

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

perm = permutations(range(1,N+1),N)
cnt = 1
count_a = 0
count_b = 0

for p in perm:
    if p == A:
        count_a = cnt
    if p == B:
        count_b = cnt
    cnt += 1

print(abs(count_a - count_b))

import itertools
N = int(input())
perm = [ x for x in itertools.permutations(range(1, N + 1)) ]
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
a = [ idx for idx, val in enumerate(perm) if val == P ][0] + 1
b = [ idx for idx, val in enumerate(perm) if val == Q ][0] + 1
print(abs(a - b))

from itertools import permutations
n = int(input())
p = input().replace(" ","")
q = input().replace(" ","")

c = permutations([i for i in range(1,n+1)],n)
for i,a in enumerate(c):
    num = "".join(map(str,a))
    # print(num)
    if p==num:pi=i+1
    if q==num:qi=i+1
print(abs(pi-qi))

def cal_A(num):
    ans = 1
    for i in range(num,0,-1):
        ans *= i
    print(ans)

def cal_B(num):
    ans = 1
    for i in range(num,0,-1):
        ans *= i
    ans = ans // num
    return ans

def cal_C(list_a):
    ans = 0
    for i in list_a:
        target_list1 = list_a[list_a.index(i):]
        target_list2 = sorted(target_list1)
        a = int(target_list2.index(i))
        ans += a * cal_B(len(target_list1))
    return ans + 1

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

print(abs(cal_C(P) - cal_C(Q)))

import math
import itertools
def i_input(): return int(input())


def i_map(): return map(int, input().split())


def i_list(): return list(map(int, input().split()))


def i_row(N): return [int(input()) for _ in range(N)]


def i_row_list(N): return [list(map(int, input().split())) for _ in range(N)]

n=i_input()
pp=tuple(i_list())
qq=tuple(i_list())
numls=[i for i in range(1,n+1)]
patters=list(itertools.permutations(numls,n))
for i in range(len(patters)):
    if pp==patters[i]:
        nmP=i
    if qq==patters[i]:
        nmQ=i
print(abs(nmQ-nmP))

from collections import deque

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

que = deque([[i] for i in range(1, N+1)])
lst = []

while que:
  seq = que.popleft()
  if len(seq) == N:
    lst.append(seq)
  else:
    for j in range(1, N+1):
      if j not in seq:
        que.append(seq + [j])

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

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

import math

def dic_order(a):
    b =[x for x in range(1, n+1)] 
    cnt = 1
    for i in range(n - 1):
        cnt += b.index(a[i]) * math.factorial(n - i - 1)
        b.remove(a[i])
    return cnt

p_order = dic_order(p)
q_order = dic_order(q)
print(abs(p_order - q_order))

import itertools
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

q = list(itertools.permutations(range(1,n+1)))
ans = 0
for i in range(len(q)):
    cnt_1 = 0
    cnt_2 = 0
    if ans == 2:
        break
    for j in range(n):
        if a[j] == q[i][j]:
            cnt_1 += 1
        if b[j] == q[i][j]:
            cnt_2 += 1
    if cnt_1 == n:
        c = i+1
        ans+= 1
    if cnt_2 == n:
        d = i+1
        ans += 1
print(abs(c-d))

import itertools

def count_order():
    # 入力
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    # 処理
    permutations_list = itertools.permutations(x for x in range(1,N+1))
    count = 0
    for one_case in permutations_list:
        count += 1
        for i in range(len(one_case)):
            if one_case[i] == P[i]:
                # a = count
                is_ok_P = True
            else:
                is_ok_P = False
                break
        if is_ok_P:
            a = count
        for i in range(len(one_case)):
            if one_case[i] == Q[i]:
                is_ok_Q = True
            else:
                is_ok_Q = False
                break
        if is_ok_Q:
            b = count
    return abs(a-b)

result = count_order()
print(result)

import itertools


N = int(input())
perm = list(itertools.permutations(list(range(1, N + 1))))
a = tuple(list(map(int, input().split())))
b = tuple(list(map(int, input().split())))

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

from itertools import *
 
N = input()
P = tuple(input().split(" "))
Q = tuple(input().split(" "))

a = 0
b = 0
c = 0
for p in permutations([str(i) for i in range(1, int(N)+1)]):
  if p==P:
    a = c
  if p==Q:
    b = c
  c += 1
print(abs(a-b))

import itertools

n=int(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))))


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

n = [i+1 for i in range(N)]

a = 0
b = 0
i = 0
for x in itertools.permutations(n):
    if list(x) == P:
        a = i
    if list(x) == Q:
        b = i
    i += 1

print(abs(a - b))

import sys
from itertools import permutations
N = int(input())
target_list = []
for i in range(2):
  target_list.append(tuple(map(int,input().split())))
  
list=[_ + 1 for _ in range(N)]
per=permutations(list,N)

ans = 0
counter = 1
if target_list[0] == target_list[1]:
  print("0")
  return
for i in per:
  if i in target_list:
    #print(counter)
    if ans == 0:
      ans -= counter
    else:
      ans += counter
    
  counter += 1
print(ans)
    


import itertools


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

n = 1
for v in itertools.permutations(list(range(1, N + 1))):
    v = list(v)
    if v == P:
        a = n
    if v == Q:
        b = n
    n += 1

print(abs(a - b))
return

import itertools
n=int(input());p,a,b=sorted(itertools.permutations(range(1,n+1),n)),[*map(int,input().split())],[*map(int,input().split())]
print(abs(([list(i)==a for i in p].index(True)+1)-([list(i)==b for i in p].index(True)+1)))

import itertools

n = int(input())
pl = list(map(int, input().split()))
ql = list(map(int, input().split()))

l = list(itertools.permutations(range(1, n+1), n))
cnt = 1
p, q = 0, 0
for i in l:
    if list(i) == pl: p = cnt
    if list(i) == ql: q = cnt
    cnt += 1

print(abs(p-q))

def dfs(pi) :
    if(len(pi) == n): 
        p.append(pi)
        return
    for i in range(1, n+1) :
        if(i in pi) :
            continue
        cpi = pi + (i,)
        dfs(cpi)
    
n = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
p = []
dfs(tuple())
i = 0

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

import itertools

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

a = b = -1
for i,x in enumerate(itertools.permutations(range(1,n+1))):
    if x == p:
        a = i
    if x == q:
        b = i

print(abs(a-b))

import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ i for i in itertools.permutations(range(1, N + 1)) ]
a = [ idx for idx, val in enumerate(ls) if P == val ][0] + 1
b = [ idx for idx, val in enumerate(ls) if Q == val ][0] + 1
print(abs(a - b))

from collections import deque
from copy import copy

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

stack = deque()
for i in range(1, N + 1):
    stack.append(([i], set([i])))
a = -1
b = -1
counter = 0
while len(stack) > 0:
    X, already = stack.pop()
    if len(X) == N:
        counter += 1
        is_a = True
        is_b = True
        for i in range(N):
            if X[i] != P[i]:
                is_a = False
            if X[i] != Q[i]:
                is_b = False
        if is_a:
            a = counter
        if is_b:
            b = counter
    else:
        for i in range(1, N + 1):
            if i in already:
                continue
            tmp = copy(X)
            tmp.append(i)
            tmp_already = copy(already)
            tmp_already.add(i)
            stack.append((tmp, tmp_already))
    if a != -1 and b != -1:
        break
print(abs(a-b))

# -*- coding: utf-8 -*-
import itertools

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

a = 0
b = 0
for i, r in enumerate(itertools.permutations(list(range(1, n+1)))):
    if r == p:
        a = i
    if r == q:
        b = i

print((abs(a - b)))


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

def kai(x):
    c = 1
    for i in range(1,x+1):
        c *= i
    return c

def dicnum(lis):
    num = [x for x in range(1,n+1)]
    count = 1
    for i in range(n-1):
        count += num.index(lis[i])*kai(n-i-1)
        num.remove(lis[i])
    return count

print(abs(dicnum(p) - dicnum(q)))

import itertools

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

L = [i for i in range(1,N+1)]
L2 = itertools.permutations(L,N)
L3 = []
for i in L2:
    L3.append(i)

# print(N,P,Q,L,L2,L3)
# print(L3.index(tuple(P)))
# print(L3.index(tuple(Q)))
print(abs(L3.index(tuple(P)) - L3.index(tuple(Q))))

from itertools import permutations 
N=int(input())


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

cnt=0

p,q=0,0

for i in permutations(range(1,N+1),N):
    cnt+=1
    if i==P:
        p=cnt
    elif i==Q:
        q=cnt
if p==0 or q==0:
    print(0)
else:        
    print(abs(p-q))    

import itertools

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

l = []
for i in range(1, n+1):
    l.append(i)
m = []
for j in itertools.permutations(l, n):
    m.append(list(j))

for a in range(9*8*7*6*5*4*3*2):
    if p == m[a]:
        x = a
        break
for b in range(9*8*7*6*5*4*3*2):
    if q == m[b]:
        y = b
        break
print(abs(x-y))

3
#coding: utf-8

from itertools import permutations

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

nth = 0
np = -1 
nq = -1
for i in permutations(list(range(N))):
    nth += 1
    s = " ".join([str(x+1) for x in i])
    if s == P:
        np = nth
    if s == Q:
        nq = nth

print((abs(np - nq)))


import itertools

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

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

print(abs(ls.index(p)-ls.index(q)))

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

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

a,b = [0,0]
for i, pair in enumerate(p,1):
  if P == pair:
    a = i
  if Q == pair:
    b = i
print(abs(a-b))

import itertools
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
p = list(itertools.permutations(range(1,N+1)))
print(abs(p.index(P)-p.index(Q)))    

import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ x for x in itertools.permutations(range(1, N + 1), N) ]
a = [ key for key, val in enumerate(ls) if val == P ][0] + 1
b = [ key for key, val in enumerate(ls) if val == Q ][0] + 1
print(abs(a - b))

import itertools
N=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
n=tuple(itertools.permutations([i for i in range(1,N+1)]))
ans=abs(n.index(p)-n.index(q))
print(ans)

import math

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

def getIndex(target):
    rtn = 0
    n_list = [i for i in range(1,N+1)]

    for p in target:
        index = n_list.index(p)

        del n_list[index]

        rtn += math.factorial(len(n_list)) * index
    return rtn


print((abs(getIndex(P)-getIndex(Q))))


N = int(input())
P = "".join(input().split())
Q = "".join(input().split())
base = [str(i) for i in range(1, N + 1)]
import itertools as itr
permutations = itr.permutations(base)
patterns = ["".join(value) for value in permutations]
patterns.sort()
p_index = patterns.index(P)
q_index = patterns.index(Q)
print(abs(p_index - q_index))

from itertools import permutations
def main():
    n = int(input())
    permutation = list(permutations([int(v) for v in range(1,n+1)]))
    ans = 0
    first = tuple(int(v) for v in  input().split())
    second = tuple(int(v) for v in  input().split())
    firstNum = None
    secondNum = None
    # print( permutation)
    for i in range(len(permutation)):
        # print(tuple(permutation[i]), first, second)
        if permutation[i] == first:
            firstNum = i+1
        if permutation[i] == second:
            secondNum = i+1
    # print(firstNum, secondNum)
    ans = abs(firstNum - secondNum)
    return ans


def __starting_point():
    print(main())
__starting_point()

import itertools
n=int(input());p,a,b=sorted(itertools.permutations(range(1,n+1),n)),map(int,input().split()),map(int,input().split())
print(abs(p.index(tuple(a))-p.index(tuple(b))))

import itertools
def II(): return int(input())
def LII(): return tuple(map(int, input().split()))

n=II()
p=LII()
q=LII()

seq = list(itertools.permutations([i for i in range(1,n+1)]))

result=abs(seq.index(p)-seq.index(q))
print(result)


import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
perm = [ i for i in itertools.permutations(range(1, N + 1)) ]
a = [ idx for idx, val in enumerate(perm) if val == P ][0] + 1
b = [ idx for idx, val in enumerate(perm) if val == Q ][0] + 1
print(abs(a - b))

import math
N=int(input())
A=list(map(lambda x:x-1,list(map(int,input().split()))))
B=list(map(lambda x:x-1,list(map(int,input().split()))))
a=0
for i in range(N):
    Apart=A[:i]
    order=0
    for aparts in Apart:
        if aparts<A[i]:
            order+=1
    a+=math.factorial(N-i-1)*(A[i]-order)
b=0
for i in range(N):
    Bpart=B[:i]
    order=0
    for bparts in Bpart:
        if bparts<B[i]:
            order+=1
    b+=math.factorial(N-i-1)*(B[i]-order)
print(abs(a-b))

from itertools import permutations

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

l = [i for i in range(1, n+1)]
list = permutations(l)

for i, l in enumerate(list):
    if P==l:
        a = i
    if Q==l:
        b = i
print(abs(a-b))

import itertools
 
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
 
X = list(itertools.permutations(P))
X.sort()
 
a=0
b=0
 
for i in range(len(X)):
    if list(X[i]) == P:
        a = i
    if list(X[i]) == Q:
        b = i
 
print(abs(a-b))

import itertools
n = int(input())
p = "".join(input().split())
q = "".join(input().split())
l = list(range(1,n+1))
i = 0
for v in itertools.permutations(l, n):
  i += 1
  j = "".join(map(str, v))
  if p == j:
    a = i
  if q == j:
    b = i
print(abs(a-b))

import itertools

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

p_num = 0
q_num = 0

for i,s in enumerate(itertools.permutations([i for i in range(1,n+1)])):
    if len([x for x,y in zip(s,p) if x==y]) == n:
        p_num = i
    if len([x for x,y in zip(s,q) if x==y]) == n:
        q_num = i
print(abs(p_num-q_num))

from itertools import permutations
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

x = permutations(sorted(p))
t =0
s = 0;
c =0;
for i,j in enumerate(x):
	if p == list(j):
		s = i+1
	if q== list(j):
		c = i+1;
		
		
		
print((abs(s-c)))


from itertools import permutations
from math import factorial
n = int(input())
p = tuple(map(int, input().split(' ')))
q = tuple(map(int, input().split(' ')))

l = [i for i in range(1, n+1)]
ls = list(permutations(l))

for i in range(factorial(n)):
    if p==ls[i]:
        a = i
    if q==ls[i]:
        b = i

print(abs(a-b))

import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
r = list(itertools.permutations([i for i in range(1,n+1)]))
P , Q = (r.index(p)), (r.index(q))
print(abs(P-Q))

from itertools import permutations
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
c = list(permutations([i for i in range(1,n+1)],n))
print(abs(c.index(p)-c.index(q)))

from itertools import permutations

N = int(input())

P = input()
Q = input()

a = 0
b = 0
for i, p in enumerate(permutations(list(range(1, N + 1))), 1):
    s = ' '.join([str(v) for v in p])
    if s == P:
        a = i
    if s == Q:
        b = i

    if a and b:
        print((abs(a - b)))
        return




from itertools import permutations

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

x = [i for i in range(1,n+1)]
ls = list(permutations(x))
i = 0
for per in ls:
    if per == p:
        a = i
    if per == q:
        b = i
    i += 1

print((abs(a-b)))


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