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
import bisect

def main():
    n = int(input())
    p = tuple(map(int,input().split()))
    q = tuple(map(int,input().split()))
    a = list(itertools.permutations(range(1,n+1)))
    print(abs(bisect.bisect_right(a,p)-bisect.bisect_right(a,q)))

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
n = int(input())
N = [int(i) for i in range(1, n+1)]
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
c = 0
a = 0; b = 0
for i in itertools.permutations(N):
    c += 1
    if i == P: a = c
    if i == Q: b = c
print(abs(a-b))

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

Solution (Python):
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P1 = sorted(P)
A = []
for x in itertools.permutations(P1):
	A.append(x)

a, b = 0, 0
for i in range(len(A)):
	if P == list(A[i]):
		a = i
	if Q == list(A[i]):
		b = i

print(abs(a-b))

Solution (Python):
import itertools

N = int(input())
P = map(int,input().split())
Q = map(int, input().split())
listX = []
for x in range(N):
    listX.append(x+1)

tupleP = tuple(P)
tupleQ = tuple(Q)

per = itertools.permutations(listX)
count = 0
countP = 0
countQ = 0

for x in itertools.permutations(listX):
    if x == tupleP:
        countP=count
    if x == tupleQ:
        countQ=count
    count += 1

print(abs(countP-countQ))

Solution (Python):
N = int(input())
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))

p_num = ""
q_num = ""
for j in range(N):
    p_num = p_num + str(p_list[j])
    q_num = q_num + str(q_list[j])
p_num = int(p_num)
q_num = int(q_num)
p_result_list = []

def perm(p, i, NUM):
    if i<NUM-1: 
        for j in range(i, NUM):
            
            t = p[i]
            p[i] = p[j]
            p[j] = t

            
            perm(p, i+1, NUM)

            
            t = p[i]
            p[i] = p[j]
            p[j] = t
    else:
        ans_num = ""
        for j in range(0,N):
            ans_num = ans_num + str(p[j])
        ans_num = int(ans_num)
        p_result_list.append(ans_num)

perm(p_list,0,N) 
p_result_list = sorted(p_result_list)
p_ord = p_result_list.index(p_num) + 1
q_ord = p_result_list.index(q_num) + 1
print(abs(p_ord-q_ord))

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

key_p = [int(y) for y in range(1, n+1)]
key_q = [int(y) for y in range(1, n+1)]
value = 1
for i in range(1, n):
  value *= i

value_p = value
value_q = value

rank_p = 1
rank_q = 1

for i in range(n):
  for j in range(len(key_p)):
    if p[i] == key_p[j]:
      rank_p += (value_p*j)
      key_p.pop(j)
      if value_p != 1:
        value_p //= (n-i-1)
      break
    else:
      continue

for i in range(n):
  for j in range(len(key_q)):
    if q[i] == key_q[j]:
      rank_q += (value_q*j)
      key_q.pop(j)
      if value_q != 1:
        value_q //= (n-i-1)
      break
    else:
      continue
print(abs(rank_p-rank_q))

Solution (Python):
n = int(input())
import itertools
orig = list(itertools.permutations( list(range(1, n+1))))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

pn = orig.index(p)
qn = orig.index(q)
print(abs(pn-qn))

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

import sys
sys.setrecursionlimit(10**8)
permu = []
def next_permutation(A,T,n):
    if n==0:
        permu.append(''.join(T))
    
    nxtlist = list(A)
    for l in nxtlist:
        A2 = set(nxtlist)
        T2 = T[:]
        A2.discard(l)
        T2.append(l)
        next_permutation(A2,T2,n-1)
    
    return 
    
    


A = [str(i) for i in range(1,N+1)]  
A = set(A)      

next_permutation(A,[],N)
permu.sort()
a,b= 0,0
ps = ''.join(P)
qs = ''.join(Q)
for i in range(len(permu)):
    if ps==permu[i]:
        a = i
    
    if qs==permu[i]:
        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()))
r = list(range(1,N+1))


a_temp = 0
b_temp = 0

for r_temp in itertools.permutations(r):
	a_temp += 1
	b_temp += 1
	if list(r_temp) == p:
		a = a_temp
	if list(r_temp) == q:
		b = b_temp
print(abs(a-b))

Solution (Python):
import itertools

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

l = list(itertools.permutations([i for i in range(1, n+1)]))
print(abs(l.index(p) - l.index(q)))

Solution (Python):
import itertools
N=int(input())
I=[str(i) for i in range(1,N+1)]
P=''.join(input().split())
Q=''.join(input().split())
li=list(itertools.permutations(I,N))

a=0
b=0
for i in range(len(li)):
    if(''.join(li[i])==P):
        a=i+1
    if(''.join(li[i])==Q):
        b=i+1
print(abs(a-b))

Solution (Python):
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
import itertools
c=itertools.permutations(range(1,n+1))
d=list(c)
x=abs(d.index(a)-d.index(b))
print(x)

Solution (Python):
import itertools

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

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








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

p = n.index(tuple(P))+1
q = n.index(tuple(Q))+1
print(abs((p - q)))

Solution (Python):
import math
import itertools
N=int(input())
Ps=list(map(int,input().split()))
Qs=list(map(int,input().split()))
sortlist=sorted(list([list(i) for i in itertools.permutations(Ps)]))
print(abs(sortlist.index(Ps)-sortlist.index(Qs)))

Solution (Python):
N=int(input())
P=int("".join(list(input().split())))
Q=int("".join(list(input().split())))

import itertools
A=[]
for i in range(1,N+1):
    A.append(i)

B=[]
for j in itertools.permutations(A,N):
    B.append(int("".join(map(str,j))))

p=0
q=0
for k in range(len(B)):
    if B[k]<P:
        p+=1
    if B[k]<Q:
        q+=1
print(abs(p-q))

Solution (Python):
import sys
import itertools

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

l = list(itertools.permutations([i for i in range(1, n+1)], n))
x = sorted([l.index(p)+1, l.index(q)+1], reverse=True)
print(x[0] - x[1])

Solution (Python):
import itertools
N = int(input())
j = [i + 1 for i in range(N)]
P = []
temp = input()
P = tuple([int(i) for i in temp.split()])
temp = input()
Q = tuple([int(i) for i in temp.split()])

J = [i for i in itertools.permutations(j)]
a = J.index(P)
b = J.index(Q)
print(abs(a - b))

Solution (Python):
import itertools


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

l = list(itertools.permutations(p))

l.sort()

p_i = 0
q_i = 0

for i in range(len(l)):
    if list(l[i]) == p:
        p_i = i
    if list(l[i]) == q:
        q_i = i

print(abs(p_i - q_i))

Solution (Python):
import math

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


a = 1
remain = list(range(1, N+1))
for i in range(N-1):
    ind = remain.index(p[i])
    remain.remove(p[i])
    a += math.factorial(len(remain)) * ind


b = 1
remain = list(range(1, N + 1))
for i in range(N - 1):
    ind = remain.index(q[i])
    remain.remove(q[i])
    b += math.factorial(len(remain)) * ind

print(abs(a-b))

Solution (Python):
import itertools

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

initial_num = 1
initial_list = []
permutation_list = []

for i in range(N):
    initial_list.append(initial_num + i)

for i in itertools.permutations(initial_list):
    permutation_list.append(list(i))

permutation_list.sort()
dictionary_P = permutation_list.index(P)
dictionary_Q = permutation_list.index(Q)

print(abs(dictionary_P - dictionary_Q))

Solution (Python):
from itertools import permutations

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

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

p_idx = l.index(tuple(p))
q_idx = l.index(tuple(q))

print(abs(p_idx-q_idx))

Solution (Python):
import itertools

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

count = 0
p_count = 0
q_count = 0

for v in itertools.permutations(sorted(p), n):
    if list(v) == p:
        p_count = count
    if list(v) == q:
        q_count = count

    count += 1

print(abs(p_count - q_count))

Solution (Python):
import itertools

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

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

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

Solution (Python):
import numpy as np

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

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

a = 0
used_l = []
for i in range(N):
    a += factorial(N-1-i)*(P[i]-1-np.sum(P[i]>np.array(used_l)))
    used_l.append(P[i])

b = 0
used_l = []
for i in range(N):
    b += factorial(N-1-i)*(Q[i]-1-np.sum(Q[i]>np.array(used_l)))
    used_l.append(Q[i])

print(abs(a-b))

Solution (Python):


import itertools

n = int(input())
p = input().split()
q = input().split()

pstr = "".join(p)
qstr = "".join(q)


ary = list(map(str,range(1,n+1)))

elements = []

for element in itertools.permutations(ary, len(ary)):
    elements.append("".join(element))
    

print(abs(elements.index(pstr)-elements.index(qstr)))

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

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

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

a = tab.index(tuple(P))
b = tab.index(tuple(Q))

print(abs(a - b))

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

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

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

ret = [i for i in range(1, n + 1)]
a = 0
b = 0
perm_list = permutations(ret)
for i, j in enumerate(perm_list):
    if p == list(j):
        a = i
    if q == list(j):
        b = i

print(abs(a-b))

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

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

for i,v in enumerate(ll):
    if list(v) == p:
        p_ind = i
    if list(v) == q:
        q_ind = i

print(abs(p_ind - q_ind))

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

factorial = [1, 1] 
for i in range(2, N):
    factorial += [factorial[-1] * i]

def get_order(P, N, factorial):
    order = 0
    num = [i+1 for i in range(N)]
    for i in range(N):
        order += sum(list(map(lambda x: x < P[i], num)))* factorial[N - i - 1]
        num.remove(P[i])
    order += 1
    return order

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

Solution (Python):
from itertools import permutations

def num(a_n, c):
    a_n_number = 0
    for i in range(len(a_n)):
        now = a_n[i]
        cnt= 0
        for k in range(i+1, len(a_n)):
            if a_n[k] <  now:
                cnt += 1
        
        a_n_number += c[i] * cnt
    
    return a_n_number


def main():
    n = int(input())
    p_n = list(map(int,input().split()))
    q_n = list(map(int,input().split()))
    c = [1]
    for i in range(n-1):
        c = [c[0]*(i+1)] + c
        
    p_n_num = num(p_n, c)
    q_n_num = num(q_n, c)
    
    print(abs(p_n_num - q_n_num))
        
    
if __name__ == "__main__":
    main()

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

Solution (Python):
def main():
	n = int(input())
	a = tuple(map(int,(input().split())))
	b = tuple(map(int,(input().split())))

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

	import itertools
	p = list(itertools.permutations(li))

	print(abs(int(p.index(a)) - int(p.index(b))))

	
 
if __name__ == '__main__':
	main()

Solution (Python):
import itertools

n = int(input())
l = [i+1 for i in range(n)]
perm = list(map(list, list(itertools.permutations(l, n))))

p = list(map(int, input().split()))
q = list(map(int, input().split()))
print(abs(perm.index(p)-perm.index(q)))

Solution (Python):
import sys
import socket
import itertools

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('c1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    N = read_int()
    P = read_int_list()
    Q = read_int_list()
    i = 1
    for p in itertools.permutations(range(1, N+1)):
      if P == list(p):
        a = i
      if Q == list(p):
        b = i
      i += 1
    res = abs(a - b)
    print(res)


main()

Solution (Python):
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
p_num = 1
q_num = 1
for i in range(n-1):
    p_cnt = 0
    q_cnt = 0
    for j in range(i+1,n):
        if p[i] > p[j]:
            p_cnt += 1
        if q[i] > q[j]:
            q_cnt += 1
    p_num += p_cnt * math.factorial(n-i-1)
    q_num += q_cnt * math.factorial(n-i-1)
print(abs(p_num - q_num))

Solution (Python):
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=list(itertools.permutations(l))

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

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

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

Solution (Python):
import itertools

n=int(input())
p=input().split()
q=input().split()

str_p = ''.join(p)
str_q = ''.join(q)

n_list=[]
for _ in range(1,n+1):
  n_list.append(str(_))
list_t= list(itertools.permutations(n_list))

str_list=['']*len(list_t)
for i in range(len(list_t)):
  _temp = list_t[i]
  _str=''.join(_temp)
  str_list[i]=_str
str_list.sort()

dif=abs(str_list.index(str_p)-str_list.index(str_q))
print(dif)

Solution (Python):
from itertools import permutations
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
tem1 = 0
tem2 = 0
for i, v in enumerate(permutations(range(1,N+1))):
  if list(v) == P: tem1 = i
  if list(v) == Q: tem2 = i
print(abs(tem1-tem2))

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

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

Pn=list(range(N) )
Qn=Pn
ans=0
for i in range(N):
    P_ind=Pn[P[i]-1]
    Q_ind=Qn[Q[i]-1]
    ans+=(P_ind-Q_ind)*math.factorial(N-i-1)
    Pn=[s if ind < P[i]-1 else s-1 for ind, s in enumerate(Pn)]
    Qn=[s if ind < Q[i]-1 else s-1 for ind, s in enumerate(Qn)]
    
ans=abs(ans)
print(ans)

Solution (Python):
import itertools

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

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

for i,v in enumerate(ll):
    if list(v)==p:
        p_ind = i
    if list(v)==q:
        q_ind = i

print(abs(p_ind - q_ind))

Solution (Python):
import math

N = int(input())

tmp1 = input().split()
tmp2 = input().split()

P = []
Q = []

P_tmp = []
Q_tmp = []

for i in range(N):
  tmp11 = int(tmp1[i])
  tmp22 = int(tmp2[i])
  
  P.append(tmp11)
  P_tmp.append(tmp11)
  P_tmp.sort()
  Q.append(tmp22)
  Q_tmp.append(tmp22)
  Q_tmp.sort()

sumP = 1
sumQ = 1

for i in range(N-1):
  nanban = P_tmp.index(P[i])
  tmp = nanban*math.factorial(N-i-1)
  if tmp < 0:
    tmp = 0
  sumP += tmp
  P_tmp.pop(nanban)
  
for i in range(N-1):
  nanban = Q_tmp.index(Q[i])
  tmp = nanban*math.factorial(N-i-1)
  if tmp < 0:
    tmp = 0
  sumQ += tmp 
  Q_tmp.pop(nanban)
 
print(abs(sumP - sumQ))

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


import itertools
l = [i for i in range(1,n+1)]
ll = itertools.permutations(l)
for i,v in enumerate(ll):
    if list(v) == p:
        pi = i + 1
    if list(v) == q:
        qi = i + 1

ans = abs(pi - qi)
print(ans)

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

def main():
    N = int(input())
    A = tuple(map(int,input().split()))
    B = tuple(map(int,input().split()))
    l = [i+1 for i in range(N)]
    count = 0
    for i in itertools.permutations(l):
        if i == A:
            a = count
        if i == B:
            b = count
        count += 1
            
    print(abs(a-b))
    
    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
import itertools
l = [i for i in range(1, n+1)]
p_list = list(itertools.permutations(l, n))
C = []
for i in p_list:
    C.append(list(i))

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

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

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

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

def g(a):
    if a == []:
        return(1)
    else:
        b = sorted(a)
        for i in range(0,len(a)):
            if a[0] == b[i]:
                a.pop(0)
                return(i * math.factorial(len(a)) + g(a))

print(abs(g(P)-g(Q)))

Solution (Python):
import itertools

N = int(input())

listing=[]

for i in range(2):
    try:
        listing.append(tuple(map(lambda x: x-1, map(int, input().split()))))
        
        
        
    except:
        break;
        

p_list = list(itertools.permutations([i for i in range(N)]))

count=0

for i in p_list:
    if i == listing[0]:
        a=count
    if i == listing[1]:
        b=count
    count+=1


print(abs(a-b))

Solution (Python):
import math
import itertools

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

li = list(itertools.permutations(P,N))
li.sort()

a = 0
b = 0

for i in range(math.factorial(N)):
    if li[i] == P:
        a = i
    else:
        a = a

for i in range(math.factorial(N)):
    if li[i] == Q:
        b = i
    else:
        b = b
        
print(abs(a-b))

Solution (Python):
import itertools

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

N_list = [i for i in range(1,N+1)]
conbination = list(itertools.permutations(N_list, N))

a = conbination.index(tuple(P))
b = conbination.index(tuple(Q))

print(abs(a-b))

Solution (Python):

import itertools
import sys


def solve(N: int, P: "List[int]", Q: "List[int]"):
    for i, p in enumerate(itertools.permutations(range(1, N+1))):
        if P == list(p):
            pp = i
        if Q == list(p):
            qq = i
    print(abs(pp-qq))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    P = [int(next(tokens)) for _ in range(N)]  
    Q = [int(next(tokens)) for _ in range(N)]  
    solve(N, P, Q)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations

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

S=sorted(P)

S=list(permutations(S))
A=[]
for i in S:
    A.append(int("".join(i)))

A.sort()

for i in range(len(A)):
    if A[i]==int("".join(P)):
        pp=i
    if A[i]==int("".join(Q)):
        qq=i
print(abs(pp-qq))

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

import itertools

A = [i+1 for i in range(N)]
all_permutation_list = list(itertools.permutations(A))
a = all_permutation_list.index(P)
b = all_permutation_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()))

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

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

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

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

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

print(abs(a - b))

Solution (Python):
import itertools

n = list(i for i in range(1, int(input()) + 1))
s1 = tuple(map(int, input().split()))
s2 = tuple(map(int, input().split()))
a, b = 0, 0
perm = list(itertools.permutations(n))
for i, v in enumerate(perm, start=1):
    if v == s1:
        a += i
    if v == s2:
        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()))

T = [i + 1 for i in range(N)]
R = itertools.permutations(T)

def equal(X, Y):
    for x, y in zip(X, Y):
        if x != y:
            return False
    return True

p = -1
q = -1
idx = 0

for r in R:


    if equal(r, P):
        p = idx
    if equal(r, Q):
        q = idx
    idx += 1

print(abs(p - q))

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

import itertools

all_list=list(itertools.permutations(range(1,n+1)))
perm_p=all_list.index(tuple(p))
perm_q=all_list.index(tuple(q))

print(abs(perm_p-perm_q))

Solution (Python):
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
lis1 = [i for i in range(1,N+1)]
lis2 = [i for i in range(1,N+1)]
def number(a, lis):
  total_order = 1
  for i in range(N):
    tmp_order = lis.index(a[i])
    lis.remove(a[i])
    total_order += tmp_order * math.factorial(len(lis))
  return total_order
print(abs(number(P, lis1) - number(Q, lis2)))

Solution (Python):
from itertools import permutations

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

dict_order = permutations(range(1, N + 1))

for n, i in enumerate(dict_order):
    if P == i:
        a = n
    if Q == i:
        b = n

print(abs(a - b))

Solution (Python):
N = int(input())
list_P = [int(x) for x in input().split()]
list_Q = [int(x) for x in input().split()]

import itertools
all_list = list(itertools.permutations(range(1,N+1)))
a = all_list.index(tuple(list_P))
b = all_list.index(tuple(list_Q))

print(abs(a-b))

Solution (Python):
import itertools

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

junList = list(itertools.permutations(l))

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

p_index = junList.index(p)
q_index = junList.index(q)

print(abs(p_index - q_index))

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

p = [1]*N
q = [1]*N

a = 1
b = 1

for i in range(N-1):
  a += (sum(p[:P[i]-1]))*math.factorial(N-(i+1))
  b += (sum(q[:Q[i]-1]))*math.factorial(N-(i+1))
  p[P[i]-1]=0
  q[Q[i]-1]=0
  

print(abs(a-b))

Solution (Python):
n = int(input())
p_lst = list(map(int,input().split()))
q_lst = list(map(int,input().split()))
p_used = []
q_used = []
a = 0
b = 0

def factorial(n):
    answer = 1
    for i in range(1,n+1):
        answer *= i
    return answer

for i in range(n-1):
    p = p_lst[i]
    k = 0
    for l in range(len(p_used)):
        tmp = [j for j in range(1,p)]
        if p_used[l] in tmp:
            k += 1
    a += (p-1-k)*factorial(n-1-i)
    p_used.append(p)

for i in range(n-1):
    q = q_lst[i]
    k = 0
    for l in range(len(q_used)):
        tmp = [j for j in range(1,q)]
        if q_used[l] in tmp:
            k += 1
    b += (q-1-k)*factorial(n-1-i)
    q_used.append(q)

print(abs(a-b))

Solution (Python):
n = int(input())
p_lst = list(map(int,input().split()))
q_lst = list(map(int,input().split()))
p_used = []
q_used = []
a = 0
b = 0

def factorial(n):
    answer = 1
    for i in range(1,n+1):
        answer *= i
    return answer

for i in range(n-1):
    p = p_lst[i]
    k = 0
    for l in range(len(p_used)):
        tmp = [j for j in range(1,p)]
        if p_used[l] in tmp:
            k += 1
    a += (p-1-k)*factorial(n-1-i)
    p_used.append(p)

for i in range(n-1):
    q = q_lst[i]
    k = 0
    for l in range(len(q_used)):
        tmp = [j for j in range(1,q)]
        if q_used[l] in tmp:
            k += 1
    b += (q-1-k)*factorial(n-1-i)
    q_used.append(q)

print(abs(a-b))

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

ans = abs(permutations.index(P) - permutations.index(Q))
 
print(ans)

Solution (Python):
import math
N = int(input())
P = list(map(str,input().split()))
Q = list(map(str,input().split()))
r = [1]*N
R = [1]*N
a = 0
b = 0

def number(P,a,N,r):
  for i in range(N):
    k = 0
    s = int(P[i])
    for j in range(s):
      k += r[j]
    t = math.factorial(N-i-1)
    a += (k-1)*t
    r[s-1] = 0
  return a+1
p = number(P,a,N,r)
q = number(Q,b,N,R)
if p > q:
  print(p-q)
else:
  print(q-p)

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple(input().split())
Q = tuple(input().split())
p = q = 0
s = ''.join([str(i) for i in range(1, N+1)])
for idx, n in enumerate(permutations(s)):
    if P == n: p = idx
    if Q == n: q = idx
print(abs(p-q))

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

l = []
for i in range(1, n+1):
	l.append(str(i))
	
import itertools
count = 0
for x in itertools.permutations(l):
	if x == P:
		p = count
	if x == Q:
		q = count
	count += 1
	
print(abs(p-q))

Solution (Python):
import itertools as iters
N = int(input())
P = [int(i) for i in input().rstrip().split()]
Q = [int(i) for i in input().rstrip().split()]

for i,p in enumerate(iters.permutations(range(1,N+1))):
    if list(p) == P:
        P = i
    if list(p) == Q:
        Q = i

print(abs(P-Q))

Solution (Python):
import itertools
N = int(input())
P = list(input().split())
Q = list(input().split())

N_str = list(map(str, range(1, N+1)))
N_pa = list(map(lambda x: int(''.join(x)), list(itertools.permutations(N_str, N))))

p_i = N_pa.index(int(''.join(P)))
q_i = N_pa.index(int(''.join(Q)))

result = abs(p_i - q_i)
print(result)

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

from itertools import permutations
def main():
    a = 0
    b = 0
    per = list(permutations(range(1,N + 1),N))
    for i,p in zip(range(len(per)),per):
        if p == P:
            a = i
        if p == Q:
            b = i
    print(abs(a-b))
    return

if __name__ == "__main__":
    main()

Solution (Python):
import itertools
import math

n = int(input())
p = int("".join(input().split()))
q = int("".join(input().split()))

t = []
for i in range(1, n + 1):
  t.append(str(i))

l = itertools.permutations(t)
m = []
for a in l:
  m.append(int("".join(a)))

p_index = -1
q_index = -1
m.sort()
for i in range(len(m)):
  if m[i] == p:
    p_index = i
  if m[i] == q:
    q_index = i

print(abs(p_index - q_index))

Solution (Python):
from math import factorial

def dictnumber(A):
    N=len(A)
    list=[i for i in range(1,N+1)]
    s=0
    for i in range(0,N):
        if i!=N-1:
            s=s+(list.index(A[i]))*factorial(N-i-1)
        else:
            s=s+1
        list.remove(A[i])
    return s

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

print(abs(dictnumber(P)-dictnumber(Q)))

Solution (Python):
import itertools
N=int(input())
nlist=[i for i  in range(1,N+1)]
newlist=[str(i) for i in nlist]
alllist=[]
p=int("".join(input().split()))
q=int("".join(input().split()))

for i in itertools.permutations(newlist):
    alllist.append(int("".join(i)))
a=alllist.index(p)+1
b=alllist.index(q)+1
c=abs(a-b)

print(c)

Solution (Python):
import itertools

def main():
	n=int(input())
	p=list(map(int, input().split()))
	q=list(map(int, input().split()))
	
	ptr=list(itertools.permutations(list(range(1,n+1))))
	
	a=0
	b=0
	if p==q:
		print('0')
	else:
		for i in range(len(ptr)):
			if p == list(ptr[i]):
				a=a+i+1
		for j in range(len(ptr)):
			if q == list(ptr[j]): 
				b=b+j+1
		print(abs(a-b))
main()

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

Solution (Python):
import itertools
n = int(input())
x = tuple(map(int, input().split()))
y = tuple(map(int, input().split()))
result = [i for i,j in enumerate(itertools.permutations(range(1, n+1), n)) if j in {x, y}]

print(abs(result[0] - result[1])) if x != y else print(0)