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

def GenerateArray(number):
    data = []
    for i in range(number):
        data.append(i+1)
    return data

def CheckArrayOrder(data, Array, count=0):
    for i in itertools.permutations(data):
        count += 1
        if list(i) == Array:
            return count
    return False

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

data = GenerateArray(number)

print(abs(CheckArrayOrder(data, P) - CheckArrayOrder(data, Q)))

Solution (Python):
from itertools import permutations
n=int(input())
a=tuple(map(int, input().split()))
b=tuple(map(int, input().split()))
x=[i for i in range(1,n+1)]
x=list(permutations(x))
x.sort()
print(abs(x.index(a)-x.index(b)))

Solution (Python):
import itertools
N = int(input())
P_list = tuple(map(int, input().split()))
Q_list = tuple(map(int, input().split()))
raw = list(itertools.permutations(range(1, N+1)))
print(abs(raw.index(P_list) - raw.index(Q_list)))

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

def calc_(x,n):
    ans = 0
    for i in range(n-1):
        ans += x[i] - 1
        ans *= (n-1-i)
        for j in range(i+1 , n):
            if(x[j] > x[i]):
                x[j] -= 1
    return ans

print( abs(calc_(p,n) - calc_(q,n)))

Solution (Python):
import itertools

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

l = list(i for i in range(1, n+1))
j = list(list(i) for i in itertools.permutations(l, n))

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

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

def dic(z):
    
    return sorted(list(itertools.permutations(z))).index(tuple(z)) + 1
 
print(abs(dic(p) - dic(q)))

Solution (Python):
N=int(input())
P=tuple(int(i) for i in input().split())
Q=tuple(int(i) for i in input().split())

import itertools

seq = range(1,N+1)
for i,l in enumerate(itertools.permutations(seq)):
    if l == P:
        ans1 = i
    if l == Q:
        ans2 = i
print(abs(ans1-ans2))

Solution (Python):
import itertools


N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
V = [i for i in range(1,N+1)]

cnt_p = 0
cnt_q = 0
cnt = 0

for v in itertools.permutations(V, N):
    cnt += 1
    if P == v:
        cnt_p = cnt
    if Q == v:
        cnt_q = cnt

print(abs(cnt_p - cnt_q))

Solution (Python):
import itertools 


n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
kind = [i for i in range(1, n+1)]
li = list(itertools.permutations(kind))
a = li.index(p)
b = li.index(q)

print(abs(a-b))

Solution (Python):

import itertools

N=int(input())
list_P=list(map( int, input().split() ))
list_Q=list(map( int, input().split() ))


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


def check_order(list_X,list_all):
  check=""
  less_num=0
  
  for L_all in list_all:
    for i in range(N):
      if list_X[i] > L_all[i] :
        check="true"
        break
      elif list_X[i] < L_all[i]:
        break
    
    if check=="true":
      less_num += 1
      check=""
  return less_num + 1
  

a=check_order(list_P, list_all)
b=check_order(list_Q, list_all)

print(abs(a-b))

Solution (Python):
import itertools
import math

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

perm = sorted(list(itertools.permutations([str(int(i+1)) for i in range(n)])))
for i in range(math.factorial(n)):
    if list(perm[i]) == p:
        a = i + 1
    if list(perm[i]) == q:
        b = i + 1
print(abs(a-b))

Solution (Python):
import math

N = int(input())
P_list = list(map(int, input().split()))
Q_list = list(map(int, input().split()))
N_list = list(range(1, N + 1))

current_permutation = [0] * N
current_index = 0
A = 0
B = 0
def dfs(i, current_permutation):

    
    if i >= N:
        global current_index
        current_index += 1
        global A
        global B
        if current_permutation == P_list:
            A = current_index
        if current_permutation == Q_list:
            B = current_index

        if A != 0 and B != 0:
            print(abs(A - B))
            exit()

        return

    for j in range(N):
        if (N_list[j] in current_permutation) == False:
            current_permutation[i] = N_list[j]
            dfs(i + 1, current_permutation)
            current_permutation[i] = 0

dfs(0, current_permutation)

Solution (Python):
from itertools import permutations

n = int(input())
per = []
for i in permutations(range(1, n+1)):
    per.append(i)

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

a = per.index(p)
b = per.index(q)

print(abs(a-b))

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
for n, v in enumerate(itertools.permutations(range(1,N+1),  N)):
  if v == P:
    a = n
  if v == Q:
    b = n
print(abs(a-b))

Solution (Python):
from itertools import permutations

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

ls = list(permutations(range(1,N+1)))
tup_p = tuple(P)
tup_q = tuple(Q)

print(abs(ls.index(tup_p)-ls.index(tup_q)))

Solution (Python):
import itertools

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

permutations = list(itertools.permutations(range(1,n+1)))
ps = permutations.index(p)
qs = permutations.index(q)

print(abs(ps-qs))

Solution (Python):
from itertools import permutations
n=int(input())
a=tuple(map(int, input().split())) 
b=tuple(map(int, input().split())) 
l=list(i for i in permutations(range(1,n+1)))
print(abs((l.index(a))-(l.index(b))))

Solution (Python):
n=int(input())
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
l=list(range(1,n+1)[::-1])
import itertools
ll=[]
for x in itertools.permutations(l):
    ll.append(list(x))
ll.sort()
a=0
b=0
c=0
for i in ll:
    if i==p:
        a=c
    if i==q:
        b=c
    c+=1
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, per in enumerate(permutations(range(1, n+1))):
    if p == per:
        a = i
    if q == per:
        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()))

falist = [0, 1]
for i in range(2, n):
    falist.append(falist[i-1] * i)
falist = falist[::-1]

pscore = 0
qscore = 0
for i in range(n-1):
    for j in range(i, n):
        if p[i] > p[j]:
            pscore += falist[i]
for i in range(n-1):
    for j in range(i, n):
        if q[i] > q[j]:
            qscore += falist[i]

print(abs(pscore - qscore))

Solution (Python):
import itertools

n = int(input())
p = input().split()
q = input().split()
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a[:n]
p2 = str(p).strip("[]\'")
p3 = p2.replace("\'", "")
q2 = str(q).strip("[]\'")
q3 = q2.replace("\'", "")

cnt = 0
cnta, cntb = 0, 0
for i in itertools.permutations(b, n):
    cnt += 1
    if str(i).strip('()') == p3:
        cnta = cnt
    if str(i).strip('()') == q3:
        cntb = cnt
print(abs(cnta - cntb))

Solution (Python):

def sort_number(N,S):
    a=1
    for k in range(0,N,1):
        j=min(S)
        r=S.pop(0)
        if j!=r:
            l=0
            for i in S:
                if i < r:l+=1
            a+=l*math.factorial(N-k-1)
    return a

import math
N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
a=sort_number(N,P)
b=sort_number(N,Q)
c=int(math.fabs(a-b))
print(c)

Solution (Python):
import itertools

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

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

for v in itertools.permutations(l, n):
    m.append(list(v))

a = 0
b = 0
for i in range(len(m)):
    if m[i] == p:
        a = i
    if m[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()))

a = 0
b = 0
idx = 1
for i in list(itertools.permutations(range(1, n+1))):
  if p == list(i):
    a = idx
  if q == list(i):
    b = idx
    
  if a > 0 and b > 0:
    break
  idx+=1
  
print(abs(a-b))

Solution (Python):
import itertools

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

PP = tuple(P)

A = list(itertools.permutations(range(1,N+1)))
PW = (A.index(P))
QW = (A.index(Q))

print(abs(PW-QW))

Solution (Python):

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
        f = factorial(N-1)
        for i in range(N-1):
            ans += candidates.index(P[i]) * f
            candidates.remove(P[i])
            f //= N-i-1
        return ans + 1

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

    



   




if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

def surprise(x):
    
    res = 1
    for i in range(1, x +1):
        res = res * i
    return res

def zyun(x, N):
    num = 0
    for i in range(N):
        x = np.where(x >x[0], x -1, x)
        num += (x[0] -1) * surprise(x.size -1) 
        x = x[1:] 
    return num   

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

x_1 = np.empty(0, dtype = np.int32)
x_2 = np.empty(0, dtype = np.int32)

for i in P:
    x_1 = np.append(x_1, i)
for i in Q:
    x_2 = np.append(x_2, i)

print(np.abs(zyun(x_1, N) -zyun(x_2, N)))

Solution (Python):
import math

l = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

k1 = 0
for i in range(1,l):
  n = 1
  for m in a[:i]:
    if m < a[i-1]:
      n = n+1
  k1 = k1 + (a[i-1]-n)*(math.factorial(l-i))

k2 = 0
for i in range(1,l):
  n = 1
  for m in b[:i]:
    if m < b[i-1]:
      n = n+1
  k2 = k2 + (b[i-1]-n)*(math.factorial(l-i))

    
print(abs(k1 - k2))

Solution (Python):
import itertools

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

D=list(range(1,N+1))
dict=[]

for i,j in enumerate(itertools.permutations(D)):
    j=list(j)
    if j==P:
        p=i
    if j==Q:
        q=i
print(abs(p-q))

Solution (Python):

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

Solution (Python):
from itertools import permutations


class Solution:
    def CountOrder(self, sequence1, sequence2):
        l = list(permutations(range(1, n + 1)))
        index1 = l.index(sequence1)
        index2 = l.index(sequence2)
        return abs(index1 - index2)


if __name__ == "__main__":
    n = int(input())
    sequence1 = tuple(map(int, input().split()))
    sequence2 = tuple(map(int, input().split()))
    solution = Solution()
    print(solution.CountOrder(sequence1, sequence2))

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

def countdic(n):
    cn=1
    p = 1
    for j in range(N-1):
        cn = sum(n[j]>x for x in n[j+1:len(n)])
        p +=cn*math.factorial(len(n)-1-j)
    return p
print(abs(countdic(P)-countdic(Q)))

Solution (Python):


from itertools import permutations

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

perms = tuple(permutations(range(1, N + 1)))
p = perms.index(P)
q = perms.index(Q)
print(abs(p - q))

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

Solution (Python):
import itertools
n = list(range(1, int(input())+1))
p, q = input(), input()
a = [str(x) for x in n]
new_list = []
for x in itertools.permutations(a):
    new_list.append(" ".join(x))
print(abs(new_list.index(p) - new_list.index(q)))

Solution (Python):
from itertools import*
n,a,b=eval('eval(input().replace(" ",",")),'*3)
i=list(permutations(range(1,n+1))).index
print(abs(i(a)-i(b)))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
n = int(input())
per = []
p = list(map(str,input().split()))
q = list(map(str,input().split()))
def dfs(num):
    if len(num) == n:
        per.append(int(num))
        
    for i in range(1,n+1):
        if str(i) not in num:
            dfs(num+str(i))
            
        
dfs("")
p = "".join(p)
q = "".join(q)
p = int(p)
q = int(q)
a = per.index(p)
b = per.index(q)
print(abs(a-b))

Solution (Python):
import itertools
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ansa=0
ansb=0

q=list(itertools.permutations(range(1,n+1)))
for i in range(len(q)):
  if a==list(q[i]):
    ansa=i+1
  if b==list(q[i]):
    ansb=i+1
print(abs(ansa-ansb))

Solution (Python):
import numpy as np
import itertools

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

seq = [str(i+1) for i in range(N)]
  
permutation = list(itertools.permutations(seq))


P_index = permutation.index(P)
Q_index = permutation.index(Q)

answer = abs(Q_index-P_index)
print(int(answer))

Solution (Python):
import itertools

N=int(input())
P=int("".join(input().split()))
Q=int("".join(input().split()))
A=[]
for i in range(N):
    A.append(str(i+1))
B=list(itertools.permutations(A))

k=0
while int("".join(B[k]))!=P:
    k+=1
j=0
while int("".join(B[j]))!=Q:
    j+=1
print(abs(k-j))

Solution (Python):
from itertools import permutations

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

def rank(a):
  return sum([tu<a for tu in list(permutations(range(1,n+1),n))])

print(abs(rank(p)-rank(q)))

Solution (Python):
import numpy as np
import itertools

N = int(input())
P = [0] * N
Q = [0] * N
P = tuple([int(i) for i in input().split()])
Q = tuple([int(i) for i in input().split()])

num_list = [i+1 for i in range(N)]
a = list(itertools.permutations(num_list)).index(P)
b = list(itertools.permutations(num_list)).index(Q)
print(abs(a - b))

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)))
a = l.index(p)
b = l.index(q)
 
print(abs(a-b))

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

Solution (Python):
import itertools

N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
nlis=[i for i in range(1,N+1)]

j=list(itertools.permutations(nlis))
cp=j.index(P)+1
cq=j.index(Q)+1
print(abs(cp-cq))

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

Solution (Python):
import itertools


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

Solution (Python):
def fact(a):
    ret = 1
    for i in range(2,a+1):
        ret *= i
    return ret

def count(a,lst):
    cnt = 0
    for atr in lst:
        if atr < a:
            cnt += 1
    return cnt
N = int(input())
Ps = list(map(int,input().split()))
Qs = list(map(int,input().split()))

a = 1
temp = 0
cntP = 1
for i in range(N):
    if N - 1 - i != 0:
        cntP = cntP + fact(N - 1 - i) * count(Ps[i],Ps[i+1:])

cntQ = 1
for i in range(N):
    if N-1-i != 0:
        cntQ = cntQ + fact(N - 1 - i) * count(Qs[i],Qs[i+1:])

print(abs(cntP-cntQ))

Solution (Python):
N = int(input())
P = input().split()
P = [int(i) for i in P]
Q = input().split()
Q = [int(i) for i in Q]

Nlist = [i+1 for i in range(N)]
k = 1
for i in range(N-1):
  k = k * (i+1)
count = 1
for i in range(N):
  n = Nlist.index(P[i])


  count = n * k + count
  Nlist.remove(P[i])
  if i == N-1:
    break
  else:
    k = k / (N-(i+1))

    
    
Nlist2 = [i+1 for i in range(N)]
k2 = 1
for i in range(N-1):
  k2 = k2 * (i+1)
count2 = 1
for i in range(N):
  n2 = Nlist2.index(Q[i])


  count2 = n2 * k2 + count2
  Nlist2.remove(Q[i])
  if i == N-1:
    break
  else:
    k2 = k2 / (N-(i+1))





if count >= count2:
  print(int(count - count2))
if count < count2:
  print(int(count2 - count))

Solution (Python):
import itertools

N=int(input())
P=tuple(map(int, input().split()))
Q=tuple(map(int, input().split()))
A=list(range(1,N+1))

Pattern=[]
for i in itertools.permutations(A):
    Pattern.append(i)

c1=Pattern.index(P) + 1
c2=Pattern.index(Q) + 1

print(abs(c1-c2))

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)))
p = l.index(P)
q = l.index(Q)
print(abs(p-q))

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

def get_order(a, n):
    res = 0
    check = [1] * (n + 1)
    check[0] = 0
    for i, c in enumerate(a):
        s = sum(check[:c])
        res += s * f[n - i - 1]
        check[c] = 0
    return res

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

print(abs(get_order(P, N) - get_order(Q, N)))

Solution (Python):
import itertools


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

junretsu = itertools.permutations(range(1, n + 1))
junretsu = sorted(junretsu)
a = junretsu.index(p)
b = junretsu.index(q)

print(abs(a - b))

Solution (Python):
import random as rng
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)


def dump_impl(*objects):
    print(*objects, file=sys.stderr)


def dump_dummy(*objects):
    pass


dump = dump_impl if "DEBUG" in sys.argv else dump_dummy

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

factrials = it.permutations(range(1, N+1), N)
a = -1
b = -1
for i, fact in enumerate(it.permutations(range(1, N+1), N)):
    dump(list(zip(list(fact), P)), list(zip(list(fact), Q)))
    if all([x == y for (x, y) in zip(list(fact), P)]):
        a = i
        dump("  a=", a)
    if all([x == y for (x, y) in zip(list(fact), Q)]):
        b = i
        dump("  b=", b)
    dump(fact)
print(abs(a-b))

Solution (Python):
import itertools
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls1 = ril()
ls2 = ril()

patterns = list(itertools.permutations(range(1, n + 1)))
index1 = patterns.index(tuple(ls1))
index2 = patterns.index(tuple(ls2))
print(abs(index1 - index2))

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

from itertools import permutations as per
tuples = per(range(1, N+1))

a = 0
b = 0
for i, t in enumerate(tuples):
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a - b))

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

import itertools
L = list(itertools.permutations(l))

jisyo = []
for i in range(len(L)):
  n = 0
  for j in range(N):
    n = n * 10 + L[i][j]
  jisyo.append(n)

p = 0
q = 0
for x in range(N):
  p = p * 10 + P[x]
  q = q * 10 + Q[x]

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

Solution (Python):
from itertools import*;(n,),a,b=eval('tuple(map(int,input().split())),'*3);i=list(permutations(range(1,n+1))).index;print(abs(i(a)-i(b)))

Solution (Python):
import itertools

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

    p = 0
    q = 0
    count = 0
    for i in itertools.permutations([i+1 for i in range(N)]):
        if P == list(i):
            p = count + 1
        if Q == list(i):
            q = count + 1
        count += 1
    print(abs(p-q))

    return 0
if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations as p
N=int(input())
l=list(p(range(1,N+1)))

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

fa=True
fb=True
for i in range(len(l)):
  if l[i]==A and fa:
    idx_a=i
    fa=False
  if l[i]==B and fb:
    idx_b=i
    fb=False
  if (not fa) and (not fb):
    break
print(abs(idx_a-idx_b))

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple([int(i) for i in input().split()])
Q = tuple([int(i) for i in input().split()])

a, b = 0, 0
for i, v in enumerate(permutations(range(1, N+1), N)):
    if v == P:
        a = i
    if v == Q:
        b = i

print(abs(a-b))

Solution (Python):
from math import factorial

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

def f(X):
    res = 1
    A = [i+1 for i in range(N)]
    for i in range(N):
        A.sort()
        if X[i] != min(A):
            res += factorial(N-(i+1)) * (A.index(X[i]))
        del A[A.index(X[i])]
    return res

print(abs(f(P)-f(Q)))

Solution (Python):
from itertools import*;n,*a=open(0);a,b=[list(permutations(range(1,int(n)+1))).index(tuple(map(int,t.split())))for t in a];print(abs(a-b))

Solution (Python):
import math
import numpy as np

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

    pe = getal(p)+1
    qe = getal(q)+1
    print(abs(pe-qe))

def getal(arr):
    tarr = arr.copy()
    arg = [None]*len(arr)
    
    c = 0
    for i in range(len(arr)):
        arg[arr.index(min(tarr))] = c
        tarr.remove(min(tarr))
        c += 1

    x = math.factorial(len(arr)-1)

    v = arg[0]*x
    

    if len(arr) == 2:
        return v
    else:
        ne = arr[1:]
        return v + getal(ne)

if __name__ == "__main__":
    main()

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

Solution (Python):
from itertools import*;(n,),a,b=eval('tuple(map(int,input().split())),'*3);i=list(permutations(range(1,n+1))).index;print(abs(i(a)-i(b)))

Solution (Python):
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
import itertools
seq=tuple([i+1 for i in range(n)])
i=0
for j in itertools.permutations(seq):
  if p==j:
    x=i
  if q==j:
    y=i
  i+=1
print(abs(x-y))

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

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

p=i.index(P)
q=i.index(Q)
print(abs(p-q))

Solution (Python):
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
import itertools
seq=tuple([i+1 for i in range(n)])
i=0
for j in itertools.permutations(seq):
  if p==j:
    x=i
  if q==j:
    y=i
  i+=1
print(abs(x-y))

Solution (Python):
import itertools
import math

n = int(input())


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


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

b = []
for i in range(1,len(a)+1):
    b.append(i)

it = itertools.zip_longest(b, a)

pcount = 0
dcount = 0

for i in it:
    if p != d:
        if p == list(i[1]):
            pcount = int(i[0])
        if d == list(i[1]):
            dcount = int(i[0])


print(abs(pcount-dcount))

Solution (Python):
from itertools import permutations as p

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = y = 0
i = 1
for v in p(list(range(1, n + 1))):
  if list(v) == a:
    x = i
  if list(v) == b:
    y = i
  i += 1
print(abs(x - y))

Solution (Python):
import math

N = int(input())

num_p = 1
P = [int(num) for num in input().split(' ')]
nums = [num+1 for num in range(N)]
for i, Pi in enumerate(P):
	nums.remove(Pi)
	num_small = len([num for num in nums if num < Pi])
	num_p += math.factorial(N-i-1)*num_small
        
num_q = 1
Q = [int(num) for num in input().split(' ')]
nums = [num+1 for num in range(N)]
for i, Qi in enumerate(Q):
	nums.remove(Qi)
	num_small = len([num for num in nums if num < Qi])
	num_q += math.factorial(N-i-1)*num_small

print(abs(num_p-num_q))

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

indp = 1
indq = 1


for i in range(n):
  c = 1
  for j in p[:i]:
    if (j < p[i]):
      c += 1
  indp += math.factorial((n-1)-i) * (p[i]-c)


for i in range(n):
  c = 1
  for j in q[:i]:
    if (j < q[i]):
      c += 1
  indq += math.factorial((n-1)-i) * (q[i]-c)

print(abs(indp-indq))

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

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

n = int(input())
p_list = tuple(map(int, input().split()))
q_list = tuple(map(int, input().split()))

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

for i in range(len(sorted_seq)):
  if p_list == sorted_seq[i]:
    a = i
  if q_list == sorted_seq[i]:
    b = i
print(abs(a - b))

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

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

Solution (Python):
import itertools

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

base = list(itertools.permutations(P))
base = sorted(base)

ans = abs(base.index(P) - base.index(Q))

print(ans)

Solution (Python):
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a = 0
b = 0
nn = sorted(p)
for i in range(n):
  a += (nn.index(p[i])-1) * math.factorial(n-i-1)
  nn.remove(p[i])

nnn = sorted(q)
for j in range(n):
  b += (nnn.index(q[j])-1) * math.factorial(n-j-1)
  nnn.remove(q[j])
print(abs(a-b))

Solution (Python):
import itertools
import math

n = int(input())


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


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

b = []
for i in range(1,len(a)+1):
    b.append(i)

it = itertools.zip_longest(b, a)

for i in it:
    if p != d:
        if p == list(i[1]):
            pcount = int(i[0])

        elif d == list(i[1]):
            dcount = int(i[0])

    else:
        pcount = 0
        dcount = 0
        
print(abs(pcount-dcount))

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

Solution (Python):
import itertools

n=int(input())
a=[]
b=[]
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))

aa=0
bb=0
cou=0
c=[]
for i in range(n):
  c.append(i+1)
  
for x in itertools.permutations(c):
  if a==x:
    aa=cou
  if b==x:
    bb=cou
  cou=cou+1
  
print(abs(aa-bb))

Solution (Python):

import itertools
n = int(input())
p = list(map(str,input().split()))
q = list(map(str,input().split()))
a = "".join(p)
b = "".join(q)
l = []
m = []
c = 0
d = ""
x = 0
y = 0

for num in itertools.permutations(p):
    d = "".join(num)
    m.append(d)

l = sorted(m)

x = l.index(a)
y = l.index(b)

print(abs(x-y))

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

Solution (Python):
import itertools

a = int(input())
b = input().split()
c = input().split()

def x(z):
    return sorted(list(itertools.permutations(z))).index(tuple(z)) + 1

x1 = x(b)
x2 = x(c)

print(abs(x1 - x2))

Solution (Python):
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
A = B = 0
for i,v in enumerate(list(itertools.permutations(range(1,N+1))), 1):
    if list(v) == P:
        A = i
    elif list(v) == Q:
        B = i
        continue
print(abs(A-B) if A != 0 and B != 0 else 0)