Problem:
 Score : 300 points    Problem Statement 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 1 3
1 3 2
3 1 2      Sample Output 1 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 .       Sample Input 2 8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1      Sample Output 2 17517       Sample Input 3 3
1 2 3
1 2 3      Sample Output 3 0

Solution (Python):
import itertools

N = int(input())
P = tuple([int(x) for x in input().split()])
Q = tuple([int(x) for x in input().split()])
numbers = []
for i in range(N):
    numbers.append(i + 1)
numbers = tuple(numbers)

combination = sorted(list(itertools.permutations(numbers)))

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

print(abs(a - b))

Solution (Python):
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(range(1,N+1))):
  if p == P:a=i
  if p == Q:b=i

print(abs(a-b))

Solution (Python):
from itertools import permutations

def main():
    n = int(input())
    p = tuple([int(x) for x in input().split()])
    q = tuple([int(x) for x in input().split()])
    L = sorted(list(permutations(list(range(1, n+1)), n)))
    for idx, l in enumerate(L):
        if l == p:
            a = idx
        if l == q:
            b = idx
    ans = abs(a - b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = b = 0
cnt = 0
for ptn in itertools.permutations(range(1,N+1)):
    cnt += 1
    ok1 = True
    for i in range(N):
        if P[i] != ptn[i]:
            ok1 = False
            break
    if ok1: a = cnt
    ok2 = True
    for i in range(N):
        if Q[i] != ptn[i]:
            ok2 = False
            break
    if ok2: b = cnt
print(abs(a-b))

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

def kaijou(n):
    ans = 1
    while n > 0:
        ans *= n
        n -= 1
    return ans

def calc(misiyou, num, cnt, x):
    if cnt == len(num):
        return x
    else:
        siyou = num[cnt]
        ind = misiyou.index(siyou)
        misiyou.remove(siyou)
        misiyou2 = misiyou.copy()
        x2 = x + (kaijou(len(misiyou2)-1))*(ind-1)
        return calc(misiyou2, num, cnt+1, x2)

lis1 = [i for i in range(N+1)]
a = calc(lis1, P, 0, 1)
lis2 = [i for i in range(N+1)]
b = calc(lis2, Q, 0, 1)
print(abs(a-b))

Solution (Python):

import itertools
N = int(input())
n = [i for i in range(1,N+1)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))
cou = 0
a,b = 0,0

for i in itertools.permutations(n,N):
    cou += 1
    if p == list(i) :
        a = cou 
    if q == list(i):
        b = cou

print(abs(a-b))

Solution (Python):


from itertools import permutations

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

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

perm2key = {perms[i]:i for i in range(len(perms))}

print(abs(perm2key[Q]-perm2key[P]))

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

import itertools

t=list(i+1 for i in range(n))
tt=list(itertools.permutations(t))

a=0
b=0
for c,it in enumerate(tt):
  if p==list(it):
    a = c
  if q==list(it):
    b = c

print(abs(a-b))

Solution (Python):

import math

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

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


print(abs(orderp-orderq))

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

import itertools

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

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

Solution (Python):
from itertools import permutations

n = int(input())
itr = permutations([i for i in range(1, n+1)])
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

for i, tpl in enumerate(itr):
    if tpl == p:
        val1 = i
    if tpl == q:
        val2 = i

print(abs(val1-val2))

Solution (Python):
N = int(input())
P = input().replace(" ","")
Q = input().replace(" ","")

from collections import deque
q = deque()
q.append("")

a = -1
b = -1
cnt = 0
while q:
  cnt += 1
  st = q.popleft()
  if len(st) == N:
    if st == P:
      a = cnt
    if st == Q:
      b = cnt
    continue
  for i in range(1, N + 1):
    if str(i) not in st:
      q.append(st + str(i))

print(abs(a - b))

Solution (Python):
import itertools

N = int(input())
P = input().replace(" ","")
Q = input().replace(" ","")

from collections import deque
q = deque()
q.append("")

a = -1
b = -1
cnt = 0
while q:
  cnt += 1
  st = q.popleft()
  if len(st) == N:
    if st == P:
      a = cnt
    if st == Q:
      b = cnt
    continue
  for i in range(1, N + 1):
    if str(i) not in st:
      q.append(st + str(i))

print(abs(a - b))

Solution (Python):
import itertools

N = int(input())
P = input().replace(" ","")
Q = input().replace(" ","")

from collections import deque
q = deque()
for i in range(1, N + 1):
  q.append(str(i))

a = -1
b = -1
cnt = 0
while q:
  cnt += 1
  st = q.popleft()
  if len(st) == N:
    if st == P:
      a = cnt
    if st == Q:
      b = cnt
    continue
  for i in range(1, N + 1):
    if str(i) not in st:
      q.append(st + str(i))

print(abs(a - b))

Solution (Python):
import itertools

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

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

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

Solution (Python):
from itertools import permutations

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

Solution (Python):
from itertools import permutations

N = int(input())

pertuple = tuple(permutations([x for x in range(1,N+1)]))

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

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

Solution (Python):
n = int(input())
from itertools import permutations
itr = permutations([i for i in range(1, n + 1)])
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

for i, tpl in enumerate(itr):
    if tpl == p:
        idx1 = i
    if tpl == q:
        idx2 = i
print(abs(idx1 - idx2))

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

Solution (Python):
from itertools import permutations
n = int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))

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

cnt = 1
for i in permutations(num):
    if P == list(i):
        a = cnt + 1
    if Q == list(i):
        b = cnt + 1
    cnt += 1
print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
dic = list(itertools.permutations(range(1,n+1)))
for i in range(len(dic)):
    if dic[i] == p:
        p_num = i
    if dic[i] == q:
        q_num = i
print(abs(p_num - q_num))

Solution (Python):
import itertools as it
import numpy as np
import math

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

rosen = list(it.permutations(range(1, N+1)))

isP = 0
isQ = 0

for i in range (0, len(rosen)):
    if list(rosen[i]) == P:
        isP = i+1
    if list(rosen[i]) == Q:
        isQ = i+1

print(abs(isP-isQ))

Solution (Python):
n = int(input())
from itertools import permutations
itr = permutations([i for i in range(1, n + 1)])
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, tpl in enumerate(itr):
  if tpl == p:
    idx1 = i
  if tpl == q:
    idx2 = i
print(abs(idx1 - idx2))

Solution (Python):







import itertools


import sys

def input(): return sys.stdin.readline().rstrip()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip('\n') for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]

def OutH(lst, deli=' '): print(deli.join(map(str, lst)))
def OutV(lst): print('\n'.join(map(str, lst)))

sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1

INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)


N = iIn()
P = tuple(iInLH())
Q = tuple(iInLH())

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

for i in range(len(lst)):
    if lst[i] == P:
        idx_p = i
    if lst[i] == Q:
        idx_q = i

ans = abs(idx_p - idx_q)
print(ans)

Solution (Python):
import itertools
N = int(input())
First_List = list(map(int,input().split()))
Second_List = list(map(int,input().split()))
Num_List = sorted(First_List)
F_Flg = 0
S_Flg = 0
Loop_Num = 1
for Sorted_Pair in itertools.permutations(Num_List, N):
    Search_Pair = list(Sorted_Pair)
    if (Search_Pair == First_List) & (F_Flg == 0):
        F_Flg = Loop_Num
    if (Search_Pair == Second_List) & (S_Flg == 0):
        S_Flg = Loop_Num
    
    if (F_Flg != 0) & (S_Flg != 0):
        break
    else:
        Loop_Num += 1

print(abs(F_Flg-S_Flg))

Solution (Python):
import itertools
N = int(input())
First_List = list(map(int,input().split()))
Second_List = list(map(int,input().split()))
Num_List = sorted(First_List)
F_Flg = 0
S_Flg = 0
Loop_Num = 1
for Sorted_Pair in itertools.permutations(Num_List, N):
    Search_Pair = list(Sorted_Pair)
    if (Search_Pair == First_List) & (F_Flg == 0):
        F_Flg = Loop_Num
    if (Search_Pair == Second_List) & (S_Flg == 0):
        S_Flg = Loop_Num
        
    
    Loop_Num += 1

print(abs(F_Flg-S_Flg))

Solution (Python):
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)))

Solution (Python):
from itertools import permutations
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
count = 0
a,b = 0,0
for i in permutations([j+1 for j in range(N)],N):
  count += 1
  if list(i) == P:
    a = count
    if list(i) == Q:
      b = count
      break
  elif list(i) == Q:
    b = count
  else:
    continue
print(abs(a-b))

Solution (Python):
N=int(input())

import itertools
l=[]
for i in range(1,N+1):
    l.append(i)
L=list(itertools.permutations(l,N)) 
L.sort()

P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
a=10**9
b=10**9
for i in range(len(L)):
    if L[i]==P:
        a=i
    if L[i]==Q:
        b=i
print(abs(a-b))

Solution (Python):
import itertools

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

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

print(abs(n_perm.index(tuple(p)) - n_perm.index(tuple(q))))

Solution (Python):
import itertools

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

l_pattern = list(itertools.permutations([i + 1 for i in range(N)]))
P_count_order = 0
Q_count_order = 0
for i,v in enumerate(l_pattern):
    if list(v) == P:
        P_count_order = i+1
    if list(v) == Q:
        Q_count_order = i+1
print(abs(P_count_order-Q_count_order))

Solution (Python):
import itertools
N = int(input())
P = tuple(input().replace(" ",""))
Q = tuple(input().replace(" ",""))
Nc = list(itertools.permutations(map(str,list(range(1,N+1)))))
print(abs(Nc.index(Q) - Nc.index(P)))

Solution (Python):
import sys
from copy import deepcopy


def perm(head: list, rest: list, result: list):
    if len(rest) == 0:
        
        result.append(head)
        return
    for r in rest:
        restx = deepcopy(rest)
        restx.remove(r)

        headx = deepcopy(head)
        headx.append(r)

        perm(headx, restx, result)


def main():
    N = int(input())
    p = [int(x) for x in input().split()]
    q = [int(x) for x in input().split()]
    
    res = []
    perm([], [i for i in range(1, N+1)], res)

    p_i = 0
    q_i = 0
    for i,l in enumerate(res):
        if l == p:
            p_i = i
        if l == q:
            q_i = i
    print(abs(p_i-q_i))

if __name__ == '__main__':
    main()

Solution (Python):
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
nums = [i+1 for i in range(n)]
ls = list(itertools.permutations(nums))

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

Solution (Python):
n = int(input())
p = input().replace(" ", "")
q = input().replace(" ", "")

array_list = [""]
for i in range(1, n + 1):
    array_list_next = []
    for array in array_list:
        for j in range(1, n + 1):
            if str(j) not in array:
                array_list_next.append(array + str(j))
    array_list = array_list_next

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

Solution (Python):
import itertools
n = int(input())
P = list(map(int, input().strip().split()))
Q = list(map(int, input().strip().split()))

P_s = tuple(P)
Q_s = tuple(Q)

p_n = 0
q_n = 0

j = 1
for i in itertools.permutations(range(1, n + 1), n):
    if i == P_s:
        p_n = j
    if i == Q_s:
        q_n = j
    j += 1
print(abs(p_n - q_n))

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

Solution (Python):
import sys
import math
import itertools
import statistics
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
per_lis = itertools.permutations(list(range(1,n+1)))
a,b,i=0,0,0
for elem in per_lis:
    i+=1
    if elem==p:
        a=i
    if elem==q:
        b=i
print(abs(a-b))

Solution (Python):
import sys
import math
import itertools
import statistics
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
per_lis = itertools.permutations(list(range(1,n+1)))
a=0
b=0
i=0
for elem in per_lis:
    i+=1
    if elem==p:
        a=i
    if elem==q:
        b=i
print(abs(a-b))

Solution (Python):
import itertools
n=int(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)))

Solution (Python):
from itertools import permutations

n=int(input())
plst=list(map(int,input().split()))
qlst=list(map(int,input().split()))

token=0
for x in permutations(range(1,n+1)):
    if list(x)==plst : p=token
    if list(x)==qlst : q=token
    token+=1
print(abs(p-q))

Solution (Python):
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
target= range(1,n+1)
p_len=0
q_len=0
for i,v in enumerate(itertools.permutations(target,n)):
    tmp=list(v)
    if p==tmp:
        p_len=i
    if q==tmp:
        q_len=i
print(abs(p_len-q_len))

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

Solution (Python):
import sys
input = sys.stdin.readline

l = list(__import__('itertools').permutations(range(1, int(input())+1)))
print(abs(l.index(tuple(map(int, input().split()))) - l.index(tuple(map(int, input().split())))))

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

Solution (Python):
import sys
import itertools

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
P = tuple(LI())
Q = tuple(LI())

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

p_index = li.index(P)
q_index = li.index(Q)
print(abs(p_index-q_index))

Solution (Python):
from itertools import permutations

n = int(input())
nl = [i for i in range(1, n+1)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
p = tuple(p)
q = tuple(q)
l = list(permutations(nl))
a = l.index(p)
b = l.index(q)
print(abs(a-b))

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

if P == Q:
    print(0)
    sys.exit()

perm = list(permutations(P))
perm.sort()
p, q = -1, -1
for i, pm in enumerate(perm):
    if pm == P:
        p = i
    if pm == Q:
        q = i
print(abs(p - q))

Solution (Python):
import itertools
 
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
 
l = []
a, b,num = 0,0,0
 
for m in range(1,n+1):
	l.append(m)
 
for i in itertools.permutations(l):
	num += 1
	if i == p:
		a = num
	if i == q:
		b = num
print(abs(a-b))

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

import math
ans = 0
for i in range(N):
  p = P[i]
  q = Q[i]
  for j in range(i):
    if P[i] > P[j]:
      p -= 1
    if Q[i] > Q[j]:
      q -= 1
  ans += (p-q)*(math.factorial(N-i-1))
print(abs(ans))

Solution (Python):
import itertools

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

l = []
a, b,num = 0,0,0

for m in range(1,n+1):
	l.append(m)

for i in itertools.permutations(l):
	num += 1
	if i == p:
		a = num
	if i == q:
		b = num
print(abs(a-b))

Solution (Python):
def resolve():
    '''
    code here
    '''
    import itertools
    N = int(input())
    P = tuple([int(item) for item in input().split()])
    Q = tuple([int(item) for item in input().split()])
    
    all = list(itertools.permutations(range(1,N+1)))
    a = 0
    b = 0
    for i, item in enumerate(all):
        
        if item == P:
            a = i
        if item == Q:
            b = i

    print(abs(a-b))


if __name__ == "__main__":
    resolve()

Solution (Python):
from itertools import permutations
n = int(input())
a = map(str, range(1, n+1))
a = "".join(a)
l = list(permutations(a, n))

p = tuple(input().split())
q = tuple(input().split())

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

Solution (Python):
import itertools
N = int(input())
a = [i for i in range(1,N+1)]
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

kaijo = 1
for i in range(N):
  kaijo *= (N - i)
A = list(itertools.permutations(a, N))
ansP = 0
for i in range(kaijo):
  if(list(A[i]) == P):
    ansP = i
ansQ = 0
for i in range(kaijo):
  if(list(A[i]) == Q):
    ansQ = i
print(abs(ansQ-ansP))

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

from itertools import permutations as per
a = list(per([i for i in range(1,n+1)]))

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

Solution (Python):
import math
import itertools

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

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

    ans +=1
    if list(i) == a:
        ansa = ans
    if list(i) == b:
        ansb = ans


print(abs(ansa-ansb))

Solution (Python):

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

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

Solution (Python):
from itertools import permutations

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

for i, j in enumerate(permutations(range(1, n + 1))):
    if list(j) == p:
        p_index = i
    if list(j) == q:
        q_index = i

print(abs(p_index - q_index))

Solution (Python):
import itertools

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

Solution (Python):
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
cnt = 0
a = -1
b = -1
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P and a == -1:
    a = cnt
  elif list(per) == Q and a == -1:
    a = cnt
  if list(per) == P and a != -1:
    b = cnt
  elif list(per) == Q and a != -1:
    b = cnt
print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
cnt = 0
a = -1
b = -1
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P:
    a = cnt
  if list(per) == Q:
    b = cnt

print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
cnt = 0
ans = []
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P:
    ans.append(cnt)
  elif list(per) == Q:
    ans.append(cnt)

print(max(ans)-min(ans))

Solution (Python):
from itertools import permutations

N = int(input())
p = ''.join(input().split())
q = ''.join(input().split())

l = permutations(range(1, N+1))
l2 = []
for i in l:
    l2.append(''.join(map(str, i)))
l = sorted(l2)

p_idx = 0
q_idx = 0
for n, i in enumerate(l):
    if i == p:
        p_idx = n
    if i == q:
        q_idx = n
print(abs(p_idx-q_idx))

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


def dfs(x, d, res):
    if len(x) == N + 1:
        d[0] += 1
        if x[1:] == P:
            res["P"] = d[0]
        if x[1:] == Q:
            res["Q"] = d[0]
        return

    for v in range(1, N + 1):
        if v not in x:
            x.append(v)
            dfs(x, d, res)
            x.pop()

res = {"P": 0, "Q": 0}
dfs([0], [0], res)
print(abs(res["P"] - res["Q"]))

Solution (Python):
import sys
import itertools

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    per = list(itertools.permutations(range(1, n+1)))
    t = list(map(lambda x: "".join(str(i) for i in x), per))
    p = "".join(sys.stdin.readline().split())
    q = "".join(sys.stdin.readline().split())
    a = 0
    b = 0
    for i, x in enumerate(t):
        if p == x:
            a = i
        if q == x:
            b = i
    ans = abs(a - b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
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)))

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

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

Solution (Python):
from itertools import permutations

N = int(input())

A = tuple([int(x) for x in input().split()])
B = tuple([int(x) for x in input().split()])

all_list = []
for i in permutations(range(1, N+1)):
    all_list.append(i)

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

Solution (Python):
import itertools
def main6():
    n = int(input())
    p=[str(i) for i in input().split()]
    q=[str(i) for i in input().split()]
    l = [str(i) for i in range(1,n+1)]
    k = ''.join(p)
    k1 = ''.join(q)
    p_l = list(itertools.permutations(l))
    for v in range(len(p_l)):
        u = (''.join(p_l[v]))
        if(u == k):
            a = v
        if(u == k1):
            b = v

    print(abs(a-b))

if __name__ == '__main__':
    main6()

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

nn = list(range(1,n + 1))

c = itertools.permutations(nn, n)

p_p = 0
q_p = 0
for i, j in enumerate(c):
    if list(j) == p:
        p_p = i
    if list(j) == q:
            q_p = i
print(abs(q_p - p_p))

Solution (Python):
def resolve():
    import itertools
    n = int(input())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    a = 0
    b = 0
    for i, l in enumerate(list(itertools.permutations(range(1, n+1), n))):
        l = list(l)
        if l == p:
            a = i+1
        if l == q:
            b = i+1
    print(abs(a-b))
resolve()

Solution (Python):
import itertools as itool
N=int(input())
plist=list(map(int, input().split()))
qlist=list(map(int, input().split()))
mylist=list(itool.permutations(range(1,N+1)))

print(abs(mylist.index(tuple(plist))-mylist.index(tuple(qlist))))

Solution (Python):
table = [1, 1, 2, 6, 24, 120, 720, 5040]

def factorial(n):
    return table[n]

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def factorial(n):
    table = [1, 1, 2, 6, 24, 120, 720, 5040]
    return table[n]

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= (i + 1)
    return ret

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import math

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = len(l)
        index = l.index(a)
        
        order += index * math.factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= (i + 1)
    return ret

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = len(l)
        index = l.index(a)
        
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    P = tuple(map(int,input().split(' ')))
    Q = tuple(map(int,input().split(' ')))
    Pnum = dictNum(P,N)
    Qnum = dictNum(Q,N)
    ans = (abs(Pnum-Qnum))
    print(ans)

def dictNum(A, N):
    ans = 0
    restNum = [1]*N
    for i in range(N):
        restNum[A[i]-1]=0
        ans += count(sum(restNum[:A[i]]),N-i)
    return ans + 1
def count(a,N):
    ans = factorial(N-1)*a
    if N == 1:
        ans = 0
    return ans
def factorial(num):
    if num == 0:
        return 1
    else:
        return num*factorial(num-1)
main()

Solution (Python):
def next_permutation(lst) :
    length = len(lst);
    
    for i in range(-1,-n,-1) :
        if lst[i-1] < lst[i] :
            lst[i:]=sorted(lst[i:]);
            
            for j in range(i,0) :
                if lst[j] > lst[i-1] :
                    tmp = lst[i-1];
                    lst[i-1] = lst[j];
                    lst[j] = tmp;
                    break;
            lst[i:]=sorted(lst[i:]); 
            break;   
    return
n = int(input());
p = [int(x) for x in input().split()];
q = [int(x) for x in input().split()];

init = [x for x in range(1,n+1)];
cnt = 1;
a = 0;
b = 0;

while a == 0 or b == 0 :
    if p == init :
        a = cnt;
    if q == init :
        b = cnt;
    cnt += 1;
    next_permutation(init);
    

print(abs(a-b));

Solution (Python):
def LI():
  return list(map(int, input().split()))

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

result = []
def permutations(N, cur):
  if len(cur) == N:
    result.append(cur)
    return

  for i in range(1, N+1):
    if i not in cur:
      permutations(N, cur+[i])

permutations(N,[])
result.sort()
print(abs(result.index(P) - result.index(Q)))

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
ls = [i for i in range(1,N+1)]
A = list(itertools.permutations(ls))
 
print(abs(A.index(P) - A.index(Q)))

Solution (Python):
import itertools

N = int(input())
P = list(map(str, input().split()))
Q = list(map(str, input().split()))
permutation = list(itertools.permutations(list(range(1, N+1))))
sum_list = [int("".join(map(str,x))) for x in permutation]
sorted(sum_list)

P_comb = int("".join(P))
Q_comb = int("".join(Q))

P_poji = sum_list.index(P_comb)
Q_poji = sum_list.index(Q_comb)

print(abs(P_poji-Q_poji))