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 itertools import permutations
from bisect import bisect_left

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

perm = list(permutations(p))
perm.sort()
a = bisect_left(perm, p)
b = bisect_left(perm, q)

print(abs(a- b))

Solution (Python):
from itertools import permutations

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

perm = list(permutations(p))
perm.sort()
a = perm.index(p)
b = perm.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()))


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

i = 0
for t in prm:
    i += 1
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a-b))

Solution (Python):

import itertools
import sys
sys.setrecursionlimit(10**9)


INF=10**20
def solve(N: int, P: "List[int]", Q: "List[int]"):
    X = list(range(1,N+1))
    R = []
    for r in itertools.permutations(X,N):
        R.append(int("".join(list(map(str,r)))))
        
    R.sort() 
    P = int("".join(P))
    Q = int("".join(Q))
    a,b = 0,0
    for i,r in enumerate(R):
        if r == P:
            a = i
        if r == Q:
            b = i

    print(abs(a-b))
    


    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 = [str(next(tokens)) for _ in range(N)]  
    Q = [str(next(tokens)) for _ in range(N)]  
    solve(N, P, Q)



if __name__ == "__main__":
    main()

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

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 math import factorial
from itertools import permutations

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

num = [str(i) for i in range(1, n+1)]

allnum = list(i for i in permutations(num, n))

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

Solution (Python):
from math import factorial

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


def cnt(x):
  pattern = 0
  numbers = list(i for i in range(1, n+1))
  for i in range(n):
    if x[i] > min(numbers): 
      pattern += numbers.index(x[i]) * factorial(n-i-1)
    numbers.remove(x[i])
  return pattern


print(abs(cnt(p) - cnt(q)))

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

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

from itertools import permutations

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



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

print(abs(a-b))

Solution (Python):
def generate_arr(a):
    if len(a) == N:
        arr.append(a)
        return
    for i in range(1,N+1):
        if i not in a:
            generate_arr(a+[i])
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
arr = []
generate_arr([])
arr.sort()
ans_p = 0
ans_q = 0
for i in range(len(arr)):
    if arr[i] == P:
        ans_p = i+1
    if arr[i] == Q:
        ans_q = i+1
print(abs(ans_p-ans_q))

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

Solution (Python):


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

a = 0
b = 0
cnt = 0
def dfs(A):
    global cnt
    global a
    global b
    if len(A) == N:
        cnt += 1
        if A == P:
            a = cnt
        if A == Q:
            b = cnt
        return   
    for v in range(1,N+1):
        if v in A:
            continue
        A.append(v)
        dfs(A)
        A.pop()
dfs([])
print(abs(a-b))

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

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

Solution (Python):
import itertools

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





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



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

Solution (Python):
import itertools

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

lis=sorted(p)

for i,j in enumerate(itertools.permutations(lis)):
  if list(j)==p:
    a=i
  if list(j)==q:
    b=i
    
print(abs(a-b))

Solution (Python):




import sys
from io import StringIO
import unittest
import itertools


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
1 3 2
3 1 2"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1"""
        output = """17517"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """3
1 2 3
1 2 3"""
        output = """0"""
        self.assertIO(input, output)


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

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

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

    print(abs(a-b))


if __name__ == "__main__":
    
    resolve()

Solution (Python):




import sys
from io import StringIO
import unittest
import itertools


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
1 3 2
3 1 2"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1"""
        output = """17517"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """3
1 2 3
1 2 3"""
        output = """0"""
        self.assertIO(input, output)


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

    NL = [i+1 for i in range(N)]
    IL = list(itertools.permutations(NL))

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

    print(abs(a-b))


if __name__ == "__main__":
    
    resolve()

Solution (Python):
n=int(input())
P=[int(x) for x in input().split()]
Q=[int(x) for x in input().split()]
p=tuple(P)
q=tuple(Q)
l=list(range(1,n+1))
L=[]
import itertools
for v in itertools.permutations(l):
  L.append(v)
a=L.index(p)
b=L.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()))
lis = [i for i in range(1,n+1)]
zisyo = list(itertools.permutations(lis))
print(abs(zisyo.index(tuple(p)) - zisyo.index(tuple(q))))

Solution (Python):
from itertools import permutations
if __name__ == "__main__":
    N = int(input())
    P = tuple(map(int,input().split()))
    Q = tuple(map(int,input().split()))
    arr = [i for i in range(1,N+1)]
    pattern_arr = list(permutations(arr))
    a,b = -1,-1
    for i,pattern in enumerate(pattern_arr):
        if pattern == P:
            a = i
        if pattern == Q:
            b = i
    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()))

fac = 1
for i in range(1,N+1):
    fac *= i

n = [i+1 for i in range(N)]
ptr = sorted(list(permutations(n)))

p = 0
q = 0
for i in range(fac):
    if list(ptr[i]) == P:
        p = i
    if list(ptr[i]) == Q:
        q = i

print(abs(p-q))

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

from itertools import permutations

lis = [i+1 for i in range(N)]
per = permutations(lis,N)

index = 0
for i in per:
    if i == P:
        p_index = index
    if i == Q:
        q_index = index
    index += 1
    
print(abs(p_index-q_index))

Solution (Python):
import sys
import numpy as np
from itertools import permutations

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = np.array(readline().split(), np.int64)
b = np.array(readline().split(), np.int64)

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

ans = abs((per_lst.index(tuple(a))+1) - (per_lst.index(tuple(b))+1))

print(ans)

Solution (Python):
import itertools as it
N=int(input())
W=list(range(1,N+1))
c1=-1
c2=-1
c=0
L=tuple(map(int,input().split()))
M=tuple(map(int,input().split()))
if L==M:
  print(0)
  exit()
for i in it.permutations(W,N):
  c+=1
  if i==L:
    c1=c
  elif i==M:
    c2=c
print(abs(c1-c2))

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




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

cnt = 1
for v in permutations(l,n):
    if p == v:
        a = cnt
    if q == v:
        b = cnt
    cnt += 1
print(abs(a-b))

Solution (Python):







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

if (p == q):
    print(0)
    exit()

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

t = list(itertools.permutations(seq))

ans = []
for i in range(len(t)):
    if list(t[i]) == p or list(t[i]) == q:
        ans.append(i)

print(ans[1]-ans[0])

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

Solution (Python):
import math

N = int(input())
P = list(map(int, input().split()))
p = sorted(P)
Q = list(map(int, input().split()))
q = sorted(Q)
a = 0
b = 0

import itertools
i = 1
for ptn in itertools.permutations(range(1, N+1)):
  if list(ptn)==P:
    a = i
  if list(ptn)==Q:
    b = i
  i += 1
print(abs(a-b))

Solution (Python):
import itertools
import math

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
n=0
A_list=itertools.permutations(range(1,N+1))
for A in A_list:
  n+=1
  A=list(A)
  if A==P:
    a=n
  if A==Q:
    b=n

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()))
Z_list = [n for n in range(1, N + 1)]
for i,j in enumerate(permutations(Z_list, r = N)):
    if j == P:
        a = i 
    if j == Q:
        b = i
print(abs(b - a))

Solution (Python):
import sys
import itertools
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N = I()
    P = LI()
    Q = LI()
    index = 1
    seq = [i+1 for i in range(N)]
    for i in itertools.permutations(seq, r=None):
        if P == i:
            a = index
        if Q == i:
            b = index        
        index += 1
    print(abs(a-b))

if __name__ == "__main__":
    main()

Solution (Python):
import itertools
N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))
p = itertools.permutations(range(1,N+1),N)
hashmap = {}
for i, v in enumerate(p):
    hashmap[v]=i+1
print(abs(hashmap[A]-hashmap[B]))

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

def func(n):
    if n==0 or n==1:
        return 1
    ans = n
    for j in reversed(range(2, n)):
        ans*=j
    return ans

def count(N, P):
    ans = 0
    a = [1]*N
    
    for i in range(N-1):
        ans+=sum(a[:P[i]-1])*func(N-1-i)
        a[P[i]-1]=0
    return ans

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

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

cnt=1

import itertools
for seq in itertools.permutations(range(1,n+1)):
    p_hantei=True
    q_hantei=True
    for i in range(n):
        if p[i]!=seq[i]:
            p_hantei=False
            break
    for i in range(n):
        if q[i]!=seq[i]:
            q_hantei=False
            break

    if p_hantei:
        p_num=cnt
    if q_hantei:
        q_num=cnt

    cnt+=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()))
num=list(itertools.permutations(range(1,n+1)))
cnt,c1,c2=0,0,0

for i in num:
  cnt+=1
  
  if i==p:
    c1=cnt
    
  if i==q:
    c2=cnt
    
print(abs(c1-c2))

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

Solution (Python):
import itertools

n=int(input())
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
t=[int(i) for i in range(1,n+1)]
a=b=0
for i,j in enumerate(list(itertools.permutations(t,n))):
    w=[1,1]
    for x,y in enumerate(j):
        if p[x]!=y: w[0]=0
        if q[x]!=y: w[1]=0
    if w[0]: a=i
    if w[1]: b=i

print(abs(a-b))

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

comb = list(perm(range(1, N+1)))
def find(x):
    for i, n in enumerate(comb):
        if x == n:
            return i
a = find(P)
b = find(Q)
ans = abs(a - b)
print(ans)

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
cnt, a, b = 0, 0, 0
for i in itertools.permutations(range(1, N+1)):
    if P == i:
        a = cnt
        if b != 0:
            break
    if Q == i:
        b = cnt
        if a != 0:
            break
    cnt += 1
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()))
P=tuple(map(lambda x: x-1,P))
Q=tuple(map(lambda x: x-1,Q))
l=list(permutations(range(N)))
print(abs(l.index(P)-l.index(Q)))

Solution (Python):
import itertools

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

l = list(itertools.permutations(range(1,N+1)))
tp = tuple(P)
tq = tuple(Q)
tpi = l.index(tp)
tqi = l.index(tq)
print(abs(tpi-tqi))

Solution (Python):


from typing import *
import collections
import functools
import itertools
import math
import operator
import sys

INF = float('inf')


def solve(N: int, P: "List[int]", Q: "List[int]"):
    return abs(operator.sub(*[list(itertools.permutations(range(1, N+1))).index(tuple(i)) for i in [P, Q]]))


def main():

    sys.setrecursionlimit(10 ** 6)

    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)]  
    print(f'{solve(N, P, Q)}')


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import numpy as np

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
nums=np.array(tuple(itertools.permutations(range(1, N+1), N))).tolist()

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

Solution (Python):
import itertools


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

    for i, j in enumerate(itertools.permutations(lst)):
        if list(j) == P:
            a = i
        if list(j) == Q:
            b = i

    print(abs(a - b))


if __name__ == "__main__":
    main()

Solution (Python):
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
num = 0
for i in itertools.permutations(range(1,n+1),n):
    if i == p:
        a = num
    if i == q:
        b = num
    num += 1
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)+1
b = l.index(Q)+1

print(abs(a-b))

Solution (Python):
import itertools

n = int(input())

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

init = sorted(p)



countp, countq, count = 0, 0, 0

for array in itertools.permutations(init, n):
    
    count += 1
    if(list(array) == p):
        countp = count
    if(list(array) == q):
        countq = count


print(abs(countp-countq))

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())
permutation_n=list(permutations(range(1,n+1)))
a=b=0
for i in permutation_n:
    if i<p:a+=1
    if i<q:b+=1
print(abs(a-b))

Solution (Python):
n=int(input())
per=[i+1 for i in range(n)]
from itertools import permutations
per=list(permutations(per,n))

from bisect import bisect_left
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))

print(abs(bisect_left(per,p)-bisect_left(per,q)))

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

for n in permutations(N):
    if p==n:
        x = index
    if q ==n:
        y = index

    index += 1

print(abs(x-y))

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

per = [0] + list(permutations(list(range(1,n+1))))
pnum = per.index(p)
qnum = per.index(q)
ans = abs(pnum-qnum)

print(ans)

Solution (Python):
import itertools


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

    n_list = [i+1 for i in range(n)]
    n_perm = list(itertools.permutations(n_list))
    
    for i, one in enumerate(n_perm):
        i1 = i + 1
        if one==p:
            a = i1
        if one==q:
            b = i1

    print(abs(a-b))

if __name__=="__main__":
    main()

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

Solution (Python):
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
num = [i for i in range(1,n+1)]
lst = list(itertools.permutations(num))
pos_p = 0
pos_q = 0
for i in range(len(lst)):
    temp = list(lst[i])
    if temp == p:
        pos_p = i
    if temp == q:
        pos_q = i
print(abs(pos_p - pos_q))

Solution (Python):
import itertools
a=int(input())
b=list(map(int,input().split()))
c=list(map(int,input().split()))

n=sorted(b)
m=list(itertools.permutations(n))
m=[list(i) for i in m]
print(abs(m.index(b)-m.index(c)))

Solution (Python):
from itertools import permutations


def mapt(fn, *args):
    return tuple(map(fn, *args))
  

def main():
    n = int(input())
    data = permutations(range(1, n+1), n)
    p = mapt(int, input().split(" "))
    q = mapt(int, input().split(" "))

    list1 = []
    for index, i in enumerate(data):
        if p == i or i == q:
            list1.append(index)
    print(abs(list1[0] - list1[-1]))
main()

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

cnt = 0
p_cnt = 0
q_cnt = 0
n_list = [i for i in range(1, n+1)]
for P in itertools.permutations(n_list):
    cnt += 1
    if p == list(P):
        p_cnt = cnt
    if q == list(P):
        q_cnt = cnt
    if p_cnt and q_cnt:
        print(abs(q_cnt-p_cnt))
        exit()

Solution (Python):
from itertools import permutations

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

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



x = list(permutations(a,n))

y = x.index(p)
z = x.index(q)

print(abs(y-z))

Solution (Python):
import math
def order(N,P):
  value=0
  N_ls=list(range(1,N+1))
  score=0
  for i in P:
    score+=N_ls.index(i)*math.factorial(len(N_ls)-1)
    N_ls.remove(i)
  return score

N=int(input())
P=[int(x) for x in input().split()]
Q=[int(x) for x in input().split()]
print(abs(order(N,P)-order(N,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)), N))
a = l.index(P) + 1
b = l.index(Q) + 1
print(abs(a - b))

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

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

Solution (Python):
import bisect
N = int(input())
Kaijo = [1 for _ in range(N)]
for i in range(2,N+1):
  Kaijo[-i] = Kaijo[1-i]*i

L = [i+1 for i in range(N)]
P = list(map(int,input().split()))
PN = 0
for i in range(N-1):
  k = bisect.bisect_left(L, P[i])
  L.remove(P[i])
  PN += k*Kaijo[i+1]

L = [i+1 for i in range(N)]
Q = list(map(int,input().split()))
QN = 0
for i in range(N-1):
  k = bisect.bisect_left(L, Q[i])
  L.remove(Q[i])
  QN += k*Kaijo[i+1]
ans = abs(PN-QN)
print(ans)

Solution (Python):
import itertools

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

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

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):
from itertools import permutations
import sys

N = int(sys.stdin.readline().rstrip())
P = [int(x) for x in sys.stdin.readline().rstrip().split()]
Q = [int(x) for x in sys.stdin.readline().rstrip().split()]


def nanba(N, P):
    P = tuple(P)
    for i, p in enumerate(permutations(range(1, N + 1), N)):
        
        if P == p:
            return i
    return 0

a = nanba(N, P)
b = nanba(N, Q)

print(abs(a - b))

Solution (Python):
import itertools

def dictnum(l):
  seq=list(itertools.permutations(l))
  seq.sort()

  return seq.index(tuple(l))

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

print(abs(dictnum(p)-dictnum(q)))

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = inpl()
B = inpl()
aa = 0
bb = 0
i = 0
for tmp in itertools.permutations(range(1,n+1)):
    i += 1
    if list(tmp) == A:
        aa = i
    if list(tmp) == B:
        bb = i
print(abs(aa - bb))

Solution (Python):
import itertools

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

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

a = 1
for i in list(itertools.permutations(num_list)):
    if P==i:
        break
    else:
        a += 1
b = 1
for i in list(itertools.permutations(num_list)):
    if Q==i:
        break
    else:
        b += 1

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

Solution (Python):
from itertools import permutations

n = int(input())
l = [tuple(map(int,input().split())) for _ in range(2)]
a = list(permutations(range(1,n+1)))
print(abs(a.index(l[0]) - a.index(l[1])))

Solution (Python):
import itertools 
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
w = 0
x = 0
S = []
T = []
for i in range(N):
    S.append(i+1)
for j in itertools.permutations(S):
    T.append(list(j))
a = T.index(P)
b = T.index(Q)
print(abs(a-b))

Solution (Python):
def factorial(n):
  if n in [0, 1]:
    return 1
  else:
    return factorial(n - 1) * n

def rank_of_list(A):
  rank = 0
  for i in range(len(A)):
    rank += (A[i] - 1) * factorial(len(A) - i - 1)
    A = list(map(lambda a: a - 1 if a > A[i] else a, A))
  return rank

def main():
  N = int(input())
  P = list(map(lambda p: int(p), input().split(" ")))
  Q = list(map(lambda q: int(q), input().split(" ")))
  print(abs(rank_of_list(P) - rank_of_list(Q)))

main()

Solution (Python):
import math
from itertools import permutations

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

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

Ns = []

for l in ls:
    s = ''.join([str(n) for n in l])
    Ns.append(int(s))

p = ''.join(map(str, P))
q = ''.join(map(str, Q))

a = Ns.index(int(p)) + 1
b = Ns.index(int(q)) + 1

print(abs(a-b))

Solution (Python):
import math
n = int(input())
def pos_num(l):
    l_sort = l.copy()
    l_len = len(l)
    l_sort.sort()
    if l_len == 1:
        an = 0
    else:
        
        an = (l_sort.index(l[0]))*math.factorial(l_len-1)
    
    return an 

    

ls = list(map(int, input().split()))
an = 0
for i in range(n-1):
    b = pos_num(ls[i:])
    an += b
    
    

ll = list(map(int, input().split()))
bn = 0
for i in range(n-1):
    b = pos_num(ll[i:])
    bn += b
    

print(abs(an-bn))

Solution (Python):
import itertools
n=int(input())
a=[]
for i in range(n):
    a.append(i+1)
b=[]
for per in itertools.permutations(a):
    b.append(list(per))
c=b.index(list(map(int,input().split())))
d=b.index(list(map(int,input().split())))
print(abs(c-d))

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
p = LIST()
q = LIST()
count = 0
for x in permutations(range(1, n+1), n):
    count += 1
    if all( x[j] == p[j] for j in range(n)):
        a = count
    if all( x[j] == q[j] for j in range(n)):
        b = count
print(abs(a-b))

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

jun = []
a = [(k+1) for k in range(N)]
def make(now):
  if len(now) == N:
    jun.append(now)
    return 0
  for item in a:
    if item not in now:
      make(now+ [item])
make([])

for k in range(len(jun)):
  if jun[k] == P:
    p = k
  if jun[k] == Q:
    q = k
  if p >=0 and q >=0:
    break
print(abs(p-q))

Solution (Python):
import sys
import math
import itertools
import collections

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    P = tuple(NLI())
    Q = tuple(NLI())
    
    ls = [x for x in range(1,N+1)]        

    p_list = list(itertools.permutations(ls))
    
    a,b = 0,0
    for x in range(len(p_list)):
        if P == p_list[x]:
            a = x
        if Q == p_list[x]:
            b = x
    print(abs(a-b))


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations

def same(l1, l2, n):
    for i in range(n):
        if l1[i] != l2[i]:
            return False
    return True

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

P = [p-1 for p in P]
Q = [q-1 for q in Q]

for i,pattern in enumerate(permutations(range(N))):
    if same(P, pattern, N):
        i_p = i
    if same(Q, pattern, N):
        i_q = i

print(abs(i_p - i_q))

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

for i,j in enumerate(permutations(range(1, n+1))):
    if j == p:
        a = i
        
    if j == q:
        b = i
        
print(abs(a-b))