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):
from math import factorial


def get_rank(l, n):
    s = 0
    es = set(range(1, n + 1))
    for i, e in enumerate(l):
        s += len([j for j in es if j < e]) * factorial(n - (i + 1))
        es -= set([e])
    return s + 1


def main():
    n = int(input())
    p = [int(i) for i in input().split(' ')]
    q = [int(i) for i in input().split(' ')]

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


main()

Solution (Python):
from itertools import permutations

N, *PQ = map(int, open(0).read().split())
P, Q = tuple(PQ[:N]), tuple(PQ[N:])

A = tuple(permutations(range(1, N + 1)))
a = A.index(P)
b = A.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(list(range(1,n+1))))
print(abs(l.index(p) - l.index(q)))

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

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

Solution (Python):
def wczytaj_liste(): 
    wczytana_lista = input()
    lista_znakow = wczytana_lista.split()
    ostateczna_lista = []
    for element in lista_znakow:
        ostateczna_lista.append(int(element))
    return ostateczna_lista 

def wartosc_bezwgledna(A): 
    if A >= 0: 
        return A
    else: 
        return -A 
    
def silnia(A): 
    if A == 0:
        return 0
    odp = 1
    for i in range(1, A+1):
        odp *= i
    return odp

from itertools import permutations 

def generuj_permutacje(N):
    lista = []
    if N == 0:
        return []
    if N == 1:
        return [1]
    else: 
        for i in range(1,N+1):
            lista.append(i)
    perm = permutations(lista)
    odp = []
    for i in list(perm): 
        odp.append(i)
    return odp  

def caunt_order(): 
    N = wczytaj_liste()[0]
    P = tuple(wczytaj_liste())
    Q = tuple(wczytaj_liste())
    permutacje = generuj_permutacje(N)
    for i in range(len(permutacje)):
        if P == permutacje[i]:
            nr_P = i
        if Q == permutacje[i]:
            nr_Q = i
    odp = nr_P - nr_Q
    print(wartosc_bezwgledna(odp))

caunt_order()

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

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

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

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

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

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

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

T = list(itertools.permutations(range(1, N+1), N))
for i in range(len(T)):
    if T[i] == P:
        s = i
    if T[i] == Q:
        d = i
print(abs(s-d))

Solution (Python):
import itertools
A=list(itertools.permutations(range(1,int(input())+1)))
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
print(abs(A.index(P)-A.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(list(range(1, N+1))))

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

Solution (Python):
import itertools

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



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

a = l.index(tuple(p))
b = l.index(tuple(q))


print(abs(a-b))

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

c=0
def dfs(X):
  global a,b,c
  if len(X)==N:
    c+=1
    if X==P:
      a=c
    if X==Q:
      b=c
  else:
    for i in range(1,N+1):
      if i not in X:
        X.append(i)
        dfs(X)
        X.remove(i)

dfs([])
print(abs(a-b))

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


def fac(n):
    res = 1
    for i in range(2, n+1):
        res *= i
    return res


ans = 0
for _ in range(2):
    res = 0
    use = [1] * (n + 1)
    for i in range(n):
        k = sum(use[1:p[i]])
        res += k * fac(n-i-1)
        use[p[i]] = 0
    p = q
    ans = abs(ans - res)
print(ans)

Solution (Python):
import itertools

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

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

allpat = list(itertools.permutations(seq))

p_num = 0
q_num = 0

for i in range(len(allpat)):
    if allpat[i] == p:
        p_num = i
    if allpat[i] == q:
        q_num = i

print(abs(p_num-q_num))

Solution (Python):
n = int(input())
def bub(x,l):
  if x == n:
    return [tuple(l)]
  ret = list()
  for i in range(1,n+1):
    if i not in l:
      ret += bub(x+1,l+[i])
  return ret
a = bub(0,list())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
print(abs(a.index(p) - a.index(q)))

Solution (Python):
import itertools

n = int(input())

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


per = list(itertools.permutations(seq))




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




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




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

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

px = 1
lst = [i + 1 for i in range(n)]
tmp = 1 
for i in range(n - 1):
  tmp *= i + 1
for i, x in enumerate(p[:-1]):
  idx = lst.index(x)
  lst.pop(idx)
  px += idx * tmp
  tmp //= n - i - 1

qx = 1
lst = [i + 1 for i in range(n)]
tmp = 1 
for i in range(n - 1):
  tmp *= i + 1
for i, x in enumerate(q[:-1]):
  idx = lst.index(x)
  lst.pop(idx)
  qx += idx * tmp
  tmp //= n - i - 1

print(abs(px - qx))

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

fact = [1]*10
for i in range(2,10):
    fact[i] = fact[i-1]*i

index1 = 1
l = [i for i in range(n)]
while len(p)>0:
    i = p.pop(0)
    l.remove(i)
    cnt = len([k for k in l if k < i])
    index1 += cnt * fact[len(l)]


index2 = 1
l = [i for i in range(n)]
while len(q)>0:
    i = q.pop(0)
    l.remove(i)
    cnt = len([k for k in l if k < i])
    index2 += cnt * fact[len(l)]

print(abs(index1-index2))

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


count = 1
for i in itertools.permutations([i+1 for i in range(N)]):
    if list(i) == p:
        a = count
    if list(i) == q:
        b = count
    count += 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()))

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

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

print(abs(a-b))

Solution (Python):
import itertools


def main():
    n = int(input())
    p = tuple(input().split())
    q = tuple(input().split())

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


if __name__ == "__main__":
    main()

Solution (Python):

import sys

def sort(P, Q):
    N = len(P)
    for i in range(N):
        if P[i] < Q[i]:
            return (P, Q)
        elif P[i] > Q[i]:
            return (Q, P)
    else:
        return (-1, -1)

def swap(array, i, j):
    temp = array[i]
    array[i] = array[j]
    array[j] = temp

    return array

def get_next_bigger(array):
    N = len(array)
    
    for i in reversed(range(N)):
        if array[i-1] < array[i]:
            
            minimum_number = N + 1
            minimum_element = i-1
            for j in range(i, N):
                if array[i-1] < array[j] and minimum_number > array[j]:
                    minimum_number = array[j]
                    minimum_element = j
            temp_array = swap(array, i-1, minimum_element)
            new_array = temp_array[:i]
            temp_array2 = temp_array[i:]
            temp_array2.sort()
            new_array.extend(temp_array2)
            return new_array

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


(minimum, maximum) = sort(P, Q)

if minimum == -1:
    print(0)
    sys.exit()


distance = 0
while True:
    if minimum == maximum:
        break
    else:
        distance += 1
    
    
    new_minimum = get_next_bigger(minimum)
    minimum = new_minimum

print(distance)

Solution (Python):
import itertools

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

p_idx, q_idx = -1, -1
for idx, item in enumerate(itertools.permutations(list(range(1, n+1)))):
    if p == list(item):
        p_idx = idx
    if q == list(item):
        q_idx = idx
    if p_idx > 0 and q_idx > 0:
        break

print(abs(p_idx - q_idx))

Solution (Python):
import itertools
from collections import defaultdict

def permutation(list_):
    return list(itertools.permutations(list_))

def main():
    N = int(input())
    P = "".join([str(x) for x in input().split()])
    Q = "".join([str(x) for x in input().split()])
    
    ls = [ "".join(i) for i in permutation([str(i+1) for i in range(N)])]
    dic = defaultdict(int)
    for num,i in enumerate(ls):
        dic[i] = (num+1)
    print(abs(dic[P]-dic[Q]))
    
if __name__ == '__main__':
    main()

Solution (Python):
import itertools

N = int(input())
P = "".join(input().split())
Q = "".join(input().split())
S = map(str, range(1, N+1))
A = sorted(["".join(x) for x in itertools.permutations(S)])
print(abs(A.index(P) - A.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

for v in permutations(range(1,N+1)):
    count += 1
    if all([P[i]==v[i] for i in range(N)]):
        a = count
    if all([Q[i]==v[i] for i in range(N)]):
        b = count

print(abs(a-b))

Solution (Python):
import itertools

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

permutation_list = list(itertools.permutations(N_data,N))

def cal(data):
    for i in range(len(permutation_list)):
        if data  == permutation_list[i]:
            return i + 1

a = cal(P)
b = cal(Q)

print(abs(a-b))

Solution (Python):
import itertools

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


p_turn, q_turn = 0, 0
for i, V in enumerate(list(itertools.permutations(range(1, N+1)))):
    if p_turn == 0 and all([p == v for p, v in zip(P, V)]):
        p_turn = i + 1
    if q_turn == 0 and all([q == v for q, v in zip(Q, V)]):
        q_turn = i + 1

print(abs(p_turn-q_turn))

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

def factorial(n):
    if n == 1:
        return n
    else:
        return n*factorial(n-1)

def order(n, list):
    total = 0
    for i in range(1, n):
        for j in range(i, n):
            if list[i-1] <= list[j]:
                list[j] -= 1
            else:
                pass

        total += (list[i-1]-1)*factorial(n-i)
    return total

def main():
    a = order(N, P)
    b = order(N, Q)
    if 0 <= a-b:
        print(a-b)
    else:
        print(-(a-b))

if __name__ == "__main__":
    main()

Solution (Python):


import itertools

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

P_tuple = tuple(P_list)
Q_tuple = tuple(Q_list)

P_all = list(itertools.permutations(sorted(P_list)))
Q_all = list(itertools.permutations(sorted(Q_list)))

P_num = P_all.index(P_tuple)
Q_num = P_all.index(Q_tuple)

print(abs(P_num - Q_num))

Solution (Python):
import itertools
n = int(input())
ar = tuple(map(int,input().split(" ")))
br = tuple(map(int,input().split(" ")))
cr = list(itertools.permutations(ar))
cr.sort()
a = cr.index(ar)
b = cr.index(br)
print(abs(a-b))

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


pp , qq = 0, 0
for i, j in enumerate(permutations(n)):
    if j == p:
        pp = i+1
    if j == q:
        qq = i+1
    if pp and qq:
        break
print(abs(pp-qq))

Solution (Python):
def calc(ans_list, num):
    ans_2 = []
    for a in ans_list:
        for i in num_list:
            if i not in a:
                t = a + i
                ans_2.append(t)
    if (num < n):
        return calc(ans_2, num + 1)
    return ans_2


n = int(input())
num_list = [str(i) for i in range(1, n + 1)]
p = int("".join(input().split(" ")))
q = int("".join(input().split(" ")))
ans_list = calc(num_list, 2)
ans_list = [int(i) for i in ans_list]
p_num = len([a for a in ans_list if a > p])
q_num = len([a for a in ans_list if a > q])
print(abs(p_num - q_num))

Solution (Python):
import itertools

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


listN=list(itertools.permutations(range(1,N+1)))
countP = 0
for i in range(0,len(listN)):
    if list(listN[i])==P:
        break
    else:
        countP+=1



countQ = 0
for j in range(0,len(listN)):
    if list(listN[j])==Q:
        break

    else:
        countQ+=1

print(abs(countP-countQ))

Solution (Python):
import itertools
n=int(input())
p=list(map(str, input().split()))
p_new=''.join(p)
q=list(map(str, input().split()))
q_new=''.join(q)
a=[]
pc, qc=0,0
 
for v in itertools.permutations(range(1,n+1)): 
    a_new=map(str, v)
    a_i=''.join(a_new)
    a.append(a_i)

for i in range(len(a)): 
    if p_new==a[i]:
        pc+=i
    if q_new==a[i]:
        qc+=i
    else:
        continue
print(abs(pc-qc))

Solution (Python):
import itertools

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

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

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

print(abs(Qi - Pi))

Solution (Python):



import itertools

N = int(input())
num1 = list(map(int,input().split()))
num2 = list(map(int,input().split()))
x, y = 0, 0
l = [_ + 1 for _ in range(N)]
A = []

for v in itertools.permutations(l, N):
    A.append(list(v))

x = A.index(num1)
y = A.index(num2)

print(abs(x - y))

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple(int(n) for n in input().split())
Q = tuple(int(n) for n in input().split())

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

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

Solution (Python):
import itertools

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

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

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

Solution (Python):
from itertools import permutations

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

l = [list(i) for i in permutations(N)]

P_idx = l.index(P)
Q_idx = l.index(Q)

print(abs(P_idx-Q_idx))

Solution (Python):
from itertools import permutations
n= int(input())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
per = list(permutations(range(1, n + 1)))
print(abs(per.index(a) - per.index(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)))

p = permutations.index(P)
q = permutations.index(Q)

print(abs(p - q))

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def sum(i,bit):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    def index_permutations(p):
        res = 0
        bit = [0]*(n+1)
        for i in range(len(p)):
            res += (p[i]-sum(p[i],bit))*f[n-i-1]
            j = p[i]
            while j <= n:
                bit[j] += 1
                j += j&-j
        return res
    n = I()
    p = LI()
    q = LI()
    f = [1]
    for i in range(1,n+1):
        f.append(f[-1]*i)
    print(abs(index_permutations(p)-index_permutations(q)))
    return


if __name__ == "__main__":
    solve()

Solution (Python):

from itertools import *

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

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

Solution (Python):

from itertools import *

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

Solution (Python):
import itertools as it

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

V = it.permutations(A)
i = 0
for v in V:
  if v == P:
    a = i
  if v == Q:
    b = i
  i += 1
print(abs(a-b))

Solution (Python):
import itertools
N = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
J = set(itertools.permutations(p))
J = sorted(list(J))
for i in range(len(J)):
    if list(J[i]) == p:
        a = i + 1
    if list(J[i]) == q:
        b = i + 1
print(abs(a-b))

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
from itertools import permutations
ans = []
for i,r in enumerate(permutations(range(1,n+1))):
    if all(p[j]==r[j] for j in range(n)):
        ans.append(i)
    if all(q[j]==r[j] for j in range(n)):
        ans.append(i)
ans = abs(ans[0]-ans[1])
print(ans)

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

n = int(input())
s = tuple(map(int, input().split()))
e = tuple(map(int, input().split()))

per = list(permutations(range(1,n+1)))
print(abs(per.index(e) - per.index(s)))

Solution (Python):
import itertools

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

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

P_in = a.index(P)
Q_in = a.index(Q)

print(abs(P_in - Q_in))

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


a = list(itertools.permutations(lis))

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

Solution (Python):
def gattai(a):
    tmp = ''
    for i in a:
        tmp += str(i)
    return tmp
import itertools
N = int(input())
a = [0]*N
p = gattai(list(map(int, input().split())))
q = gattai(list(map(int, input().split())))
for i in range(1,N+1):
    a[i-1] = i
ans1 = 0
ans2 = 0
count = 0
for i in itertools.permutations(a):
    count += 1
    if gattai(i) == p:
        ans1 = count
    if gattai(i) == q:
        ans2 = count
print(abs(ans1-ans2))

Solution (Python):
import itertools
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(a.index(p)-a.index(q)))

Solution (Python):
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
li = [i for i in range(1, n+1)]
j = 1
p_index = 1
q_index = 1
for v in itertools.permutations(li, n):
  if(list(v) == p):
    p_index = j
  if(list(v) == q):
    q_index = j
  j += 1
print(abs(p_index - q_index))

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

p = list(map(int, input().split()))
p_index = l.index(p)

q = list(map(int, input().split()))
q_index = l.index(q)

print(abs(p_index - q_index))

Solution (Python):
import itertools

N = int(input())
P = tuple([int(x) for x in input().split()])
S = tuple([int(x) for x in input().split()])

lst = [int(x) for x in P]
lst = sorted(itertools.permutations(lst))
print(abs(lst.index(P) - lst.index(S)))

Solution (Python):
import itertools
 
def resolve():
    N = int(input())
    P_list = tuple(map(int, input().split()))
    Q_list = tuple(map(int, input().split()))
 
    N_list = [(i+1) for i in range(N)]
    N_permutations_list = itertools.permutations(N_list)
 
    i = 1
    for i_list in N_permutations_list:
        if i_list == Q_list:
            a = i
        if i_list == P_list:
            b = i
        i += 1
 
    print(abs(a-b))
 
 
if __name__ == "__main__":
    resolve()

Solution (Python):
import math
N = int(input())
P_list = [int(i) for i in input().split()]
Q_list = [int(i) for i in input().split()]
def dic_num(N, X_list):
    now_num = N
    ans = 0
    while len(X_list)>1:
        value = (X_list[0]-1)*math.factorial(now_num-1)
        ans += value
        now_num -= 1
        for i,n in enumerate(X_list):
            if n > X_list[0]:
                X_list[i]=X_list[i]-1
        X_list.pop(0)
    return ans
print(abs(dic_num(N, P_list)-dic_num(N, Q_list)))

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

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


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

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

a = PERMUTATIONS.index(p) + 1
b = PERMUTATIONS.index(q) + 1

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

Solution (Python):
import itertools

n = int(input())
lst = [int(elem) for elem in range(1, n+1)]
p = list(itertools.permutations(lst))

ps = [int(elem) for elem in input().split()]
qs = [int(elem) for elem in input().split()]

print(abs(p.index(tuple(ps)) - p.index(tuple(qs))))

Solution (Python):
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
import itertools as it
x=0
y=0
for i,j in enumerate(it.permutations(list(range(1,n+1)))):
    if a==j:
        x=i
    if b==j:
        y=i
print(abs(x-y))

Solution (Python):
def dict_order(l): 
    import math 
    n = len(l)
    order = 0 
    used = [0]*n
    for i in range(n):
        order += (l[i]-1-sum(used[:l[i]]))*math.factorial(N-1-i)
        used[l[i]-1] += 1
    return order 
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
p = dict_order(P)
q = dict_order(Q)
print(abs(p-q))

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

d = []

def func(x):
    if len(x) == n:
        temp = 0
        for i, t in enumerate(x):
            temp += t * (n ** i)
        d.append(temp)
        return
    for i in range(1, n+1):
        if i not in x:
            func(x + [i])

func([])

d.sort()
p = list(reversed(p))
q = list(reversed(q))
a = 0
b = 0
for i in range(n):
    a += p[i] * (n ** i)
    b += q[i] * (n ** i)


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

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

def factorial(n):
	if n == 1:
		return 1
	else:
		return n * factorial( n - 1 )

def calc_index( list_of_number: list ):
	lists = list(range(1, n+1))
	ans = 1
	for i in range( 1, n ):
		index = lists.index( list_of_number[ i - 1 ] )
		ans += factorial( n - i ) * ( index )
		lists.remove( list_of_number[ i - 1 ] )
	return ans
ans_p = calc_index( p )
ans_q = calc_index( q )

print( abs( ans_p - ans_q ))

Solution (Python):
import itertools

def resolve():
    N = int(input())
    P_list = tuple(map(int, input().split()))
    Q_list = tuple(map(int, input().split()))

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

    i = 1
    for i_list in N_permutations_list:
        if i_list == Q_list:
            a = i
        if i_list == P_list:
            b = i
        i += 1

    print(abs(a-b))


if __name__ == "__main__":
    resolve()

Solution (Python):
from itertools import permutations

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

for idx, pm in enumerate(permutations(range(1, N + 1))):
    if P == pm:
        a = idx
    if Q == pm:
        b = idx

print(abs(a-b))

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())

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

try:
    x, y = [i for i, t in enumerate(ps) if t == P or t == Q]
except ValueError:
    print(0)
else:
    print(abs(x - y))

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())

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

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

Solution (Python):
import itertools

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

Solution (Python):
import itertools
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
r=list(itertools.permutations(p))
r.sort()
count1=r.index(p)
count2=r.index(q)
print(abs(count1-count2))

Solution (Python):
import itertools
import numpy as np
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

num = np.arange(1, n+1)

zyun = list(itertools.permutations(list(num), n))

for i in range(0, len(zyun)):
    if zyun[i] == p:
        a = i+1
    if zyun[i] == q:
        b = i+1

print(abs(a-b))

Solution (Python):
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N = int(sys.stdin.buffer.readline())
P = tuple(map(int, sys.stdin.buffer.readline().split()))
Q = tuple(map(int, sys.stdin.buffer.readline().split()))

p = -1
q = -1
for i, c in enumerate(list(itertools.permutations(range(1, N + 1)))):
    if c == P:
        p = i
    if c == 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()))
R=list(itertools.permutations(P))
R.sort()
count1=R.index(P)
count2=R.index(Q)
print(abs(count1-count2))

Solution (Python):
import itertools
a=int(input())
z=tuple(map(int,input().split()))
x=tuple(map(int,input().split()))
y=list(itertools.permutations(z))
y.sort()
ans1=y.index(z)+1
ans2=y.index(x)+1
print(abs(ans1-ans2))

Solution (Python):
import itertools
N = int(input())
P = tuple([int(_) for _ in input().split()])
Q = tuple([int(_) for _ in input().split()])
n = [int(i) for i in range(1, N + 1)]
jun = list(itertools.permutations(n, N))

p = jun.index(P) + 1
q = jun.index(Q) + 1

print(abs(p - q))

Solution (Python):
from heapq import heappush, heappop
from collections import deque
import re
import math
import functools
import itertools


def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


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


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29


def mk1d(n, val=INF):
    return [val for i in range(n)]

def mk2d(h, w, val=INF):
    return [[val for i in range(w)]for i in range(h)]

DIV = 998244353


def convolve(a, b):
    p, g = 1107296257, 5
    n0, n1 = len(a), len(b)
    n = 1 << (max(n0, n1) - 1).bit_length() + 1
    a = a + [0] * (n-n0)
    b = b + [0] * (n-n1)
    w = pow(g, (p - 1) // n, p)
    invw = pow(w, p-2, p)

    def fft(f):
        d = n // 2
        v = w
        while d >= 1:
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, u * (f[j] - f[j+d]) % p
                u = u * v % p
            v = v * v % p
            d //= 2

    def ifft(f):
        d = 1
        while d < n:
            v = pow(invw, n // (2 * d), p)
            u = 1
            for i in range(d):
                for j in range(i, n, 2*d):
                    f[j+d] *= u
                    f[j], f[j+d] = (f[j] + f[j+d]) % p, (f[j] - f[j+d]) % p
                u = u * v % p
            d *= 2

    fft(a), fft(b)
    for i in range(n):
        a[i] = a[i] * b[i] % p
    ifft(a)
    invn = pow(n, p - 2, p)
    return [a[i] * invn % p for i in range(n0 + n1 - 1)]


def main():
    N = iRaw()
    Ps = tuple(isRaw())
    Qs = tuple(isRaw())
    idxP = -1
    idxQ = -1
    permN = itertools.permutations(range(1,N+1),N)
    for idx,perm in enumerate(permN):
        if  idxP==-1 and perm == Ps:
            idxP = idx
        if idxQ == -1 and perm == Qs:
            idxQ = idx
    return abs(idxP-idxQ)

if __name__ == "__main__":
    print(main())