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 as perm
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a,b = 0,0

for i,tmp in enumerate(sorted(perm(range(1,n+1)))):
    if tmp[0] != p[0] and  tmp[0] != q[0]:
        continue
    for s,t in zip(tmp, p):
        if s != t:
            break
    else:
        a = i+1
    for s,t in zip(tmp, q):
        if s != t:
            break
    else:
        b = i+1
print(abs(a-b))

Solution (Python):
import itertools

N = int(input())
seq = list(range(1,N + 1))

all = itertools.permutations(seq)
all = list(all)
Ps =  list(map(int,input().split(" ")))
Ps = tuple(Ps)
Qs =  list(map(int,input().split(" ")))
Qs = tuple(Qs)
for i,li in  enumerate(all):
    if li == Ps:
        a = i
        break


for i,li in  enumerate(all):
    if li == Qs:
        b = i
        break
print(abs(a-b))

Solution (Python):
import itertools

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

pn = l.index(P)
qn = l.index(Q)
print(abs(pn-qn))

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

from itertools import permutations
li = [i for i in range(1, n + 1)]
ptn = list(permutations(li))

for itr, val in enumerate(ptn):
  if p == val:
    a = itr
  if q == val:
    b = itr

print(abs(a - b))

Solution (Python):
import itertools
n = int(input())
a = ''.join(input().split())
b = ''.join(input().split())

perm = list(itertools.permutations( list(str(i) for i in range(1, n+1)) ))
perm = list(map(''.join, perm))

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

Solution (Python):
import itertools

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

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

Solution (Python):
def main():
    from itertools import permutations
    n=int(input())
    a=tuple(map(int,input().split()))
    b=tuple(map(int,input().split()))
    
    p=list(permutations(range(1,n+1)))
    d={i:j for j,i in enumerate(p)}
    print(abs(d[a]-d[b]))


main()

Solution (Python):
from itertools import permutations

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

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

for i, pe in enumerate(per, start=1):
    if P == pe:
        a = i
    if Q == pe:
        b = i
print(abs(a - b))

Solution (Python):
from itertools import permutations

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    num_list = []
    for a in permutations(range(1,N+1)):
        num_list.append(a)
    num_list.sort
    b = -1
    c = -1
    for i, a in enumerate(num_list):
        if list(a) == P:
            b = i
        if list(a) == Q:
            c = i

        if b != -1 and c != -1:
            break

    print(abs(b-c))

    
            



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

x = sorted(p)
ab = []
for i, per in enumerate(permutations(x, n)):
    if per == p or per == q:
        ab.append(i) 

if len(ab) < 2:
    print(0)
else:
    print(abs(ab[0]-ab[1]))

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

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

Solution (Python):
from sys import stdin
import math

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
lin = list(map(int,stdin.readline().rstrip().split()))
from itertools import permutations
lis = permutations(li)

liv = []

for i in lis:
    liv.append(list(i))
liv.sort()
a = liv.index(li)
b = liv.index(lin)

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()))
X = sorted(list(permutations(list(range(1,N+1)))))
for i,x in enumerate(X):
    if x==P: a=i
    if x==Q: b=i
print(abs(a-b))

Solution (Python):
import itertools

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

base = tuple([i for i in range(1, N+1)])

a = 0
b = 0
L = list(itertools.permutations(base))
for i, l in enumerate(L):
  if l == P:
    a = i
  if l == Q:
    b = i

print(abs(a-b))

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

Solution (Python):
import itertools






from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


def main():
    n = ii()
    a = list(range(1,n+1))
    p = li()
    q = li()
    pc = 0
    qc = 0
    for e,i in enumerate(itertools.permutations(a)):
        
        lli = list(i)
        if lli == p:
            pc = e

        if lli == q:
            qc = e

    print(abs(pc-qc))


    
main()

Solution (Python):
import itertools

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

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

Solution (Python):
import sys
from itertools import permutations
inputs = sys.stdin.readlines()

n = int(inputs[0])
P = list(map(int, inputs[1].split()))
Q = list(map(int, inputs[2].split()))

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

p_index = q_index = -1
for i, x in enumerate(permutations(range(1, n+1))):
    x = ''.join(map(str, x))
    if x == p:
        p_index = i
    if x == q:
        q_index = i
    if p_index != -1 and q_index != -1:
        break
print(abs(p_index - q_index))

Solution (Python):
import itertools

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

alls = list(itertools.permutations(seed))

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

Solution (Python):
from itertools import permutations

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

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

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

Solution (Python):
import sys
import itertools
import math

input = sys.stdin.readline

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 = list(itertools.permutations(l, N))

for i in range(N):
    p_list.sort(key = lambda x: x[N - i - 1])

a = p_list.index(P)
b = p_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()))
if p == q:
  print(0)
  exit()
nums = [i for i in range(1,n+1)]
a = [0,0]
now = 0
for k,i in enumerate(itertools.permutations(nums)):
  if i == p or i == q:
    a[now] = k
    now += 1
print(abs(a[1]-a[0]))

Solution (Python):

N = int(input())

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

q = [[]]

for i in range(N):

    nq = []
    for j in range(len(q)):

        for k in range(N):
            k += 1

            if k not in q[j]:
                ad = q[j].copy()
                ad.append(k)
                nq.append(ad)

    q = nq

q.sort()

a = None
b = None

for i in range(len(q)):

    if " ".join(map(str,q[i])) == " ".join(map(str,P)):
        a = i
    if " ".join(map(str,q[i])) == " ".join(map(str,Q)):
        b = i

print (abs(a-b))

Solution (Python):
import itertools

N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
A = list(itertools.permutations(range(1,N+1)))
a = A.index(P) + 1
b = A.index(Q) + 1
print(abs(a-b))

Solution (Python):
from itertools import permutations

def solve(N, P, Q):
    l = list(permutations(range(1, N+1)))
    
    a = l.index(P)
    b = l.index(Q)
    return abs(a-b)

def main():
    N = int(input())
    P = tuple(int(x) for x in input().split())
    Q = tuple(int(x) for x in input().split())
    ans = solve(N, P, Q)
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):

import itertools
def index(p):
    for i, q in enumerate(itertools.permutations(range(1, len(p) + 1))):
        if p == q:
            return i
    assert False
def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    print(abs(index(p) - index(q)))
if __name__ == "__main__":
    main()

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

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

import itertools as iter

N = num()
P = tuple(nums())
Q = tuple(nums())
pmts = list(iter.permutations(range(1,N+1)))
pmts.sort()
a = pmts.index(P)
b = pmts.index(Q)
print(abs(a-b))

Solution (Python):
from itertools import permutations

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

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

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

Solution (Python):
import itertools

N = int(input())
P = "".join(input().split())
Q = "".join(input().split())

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

l = list(itertools.permutations(nl))
sl = []

for i in l:
    sl.append("".join(list(map(str,i))))

sl.sort()
for i in range(len(sl)):
    if (sl[i] == P):
        a = i
    if (sl[i] == Q):
        b = i

print(abs(a-b))

Solution (Python):
from itertools import permutations

N = int(input())
Plist = tuple(map(int, input().split()))
Qlist = tuple(map(int, input().split()))

perm = list(permutations(range(1, N+1)))
a = perm.index(Plist)
b = perm.index(Qlist)

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

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

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

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

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


def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    Q = [int(x) for x in input().split()]
    y = [int(x) for x in range(1, N + 1)]
    x = list(itertools.permutations(y))
    a = 0
    b = 0
    for i, v in enumerate(x):
        for j in range(N):
            if P[j] != v[j]:
                break
        else:
            a = i
        for j in range(N):
            if Q[j] != v[j]:
                break
        else:
            b = i
    print(abs(a - b))


if __name__ == '__main__':
    main()

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


p = int("".join(P))
q = int("".join(Q))


R = []
for i in range(N):
    R.append(str(i+1))

r = int("".join(R))

count = 1
for i in permutations(R):
    j = int("".join(i))
    if j == p:
        a = count
    if j == q:
        b = count
    count += 1

print(abs(b-a))

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

import itertools

L = []
for temp in itertools.permutations(P):
    L.append(temp)
    
L.sort()
for i in range(len(L)):
    if list(L[i]) == P:
        a = i
    if list(L[i]) == Q:
        b = i
        
print(abs(a-b))

Solution (Python):
from sys import stdin
import itertools
N = int(stdin.readline().rstrip())
P = [int(x) for x in stdin.readline().rstrip().split()]
Q = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = 0
ans2 = 0
cnt = 1
for v in itertools.permutations(list(range(1,N+1))):
    if v == tuple(P):
        ans1 = cnt
    if v == tuple(Q):
        ans2 = cnt
    cnt += 1
print(abs(ans1-ans2))

Solution (Python):

import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = -1
b = -1
count = 1
for x in itertools.permutations(range(1, N+1)):
    if P == list(x):
        a = count
    if Q == list(x):
        b = count
    count += 1

print(abs(a-b))

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

Solution (Python):
import itertools

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

i = 1
for l in itertools.permutations(list(range(1, n + 1))):
    if l == p:
        x = i
    if l == q:
        y = i
    i += 1

print(abs(x - y))

Solution (Python):
n=int(input())
p="".join([i for i in input().split()])
q="".join([i for i in input().split()])
hoge=[]
def dfs(q,st):
    global hoge
    if q==n:
        hoge.append(st)
    else:
        for i in range(1,n+1):
            if str(i) not in st:
                dfs(q+1,st+str(i))
dfs(0,"")
hoge.sort()
print(abs(hoge.index(p)-hoge.index(q)))

Solution (Python):
from itertools import *

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

c = 0
for p in permutations(range(1, N+1)):
  if p==P:
    a = c
  if p==Q:
    b = c
  c += 1
print abs(a-b)

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


def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ps = ""
    for i in P:
        ps = ps + str(i)
    qs = ""
    for i in Q:
        qs = qs + str(i)
    import itertools
    A = [i+1 for i in range(N)]
    i = 0
    p = q = 0
    for a in itertools.permutations(A):
        s = ""
        for j in a:
            s = s + str(j)
        if ps == s:
            p = i
        if qs == s:
            q = i
        i += 1
    print(abs(p-q))


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations

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

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

Solution (Python):
import itertools
n = int(input())
a = tuple([ int(v)-1 for v in input().split() ])
b = tuple([ int(v)-1 for v in input().split() ])
t = 0
for i in itertools.permutations(range(n)):
    if a == i:
        xa = t
    if b == i:
        xb = t
    t += 1
print(abs(xa-xb))

Solution (Python):
import itertools as it

N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
a, b = 0, 0
count = 0
for perm in it.permutations(range(1, N + 1)):
    count += 1
    if list(perm) == P:
        a = count
    if list(perm) == Q:
        b = count
print(abs(a - b))

Solution (Python):
import itertools


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

nums = [0] * N
for i in range(N):
    nums[i] = i+1

count = 1
for i in itertools.permutations(nums):
    if (list(i)==P):
        c1 = count
    if (list(i)==Q):
        c2 = count
    count +=1

print(abs(c2-c1))

Solution (Python):
import itertools

N = int(input().strip())
P = ''.join(input().strip().split(' '))
Q = ''.join(input().strip().split(' '))


all = sorted(''.join(p) for p in itertools.permutations(P))
print(abs(all.index(P) - all.index(Q)))

Solution (Python):
from itertools import permutations

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = j = -1
cur = 0
for c in permutations(range(1, n + 1)):
    c = list(c)
    if c == b: j = cur
    if c == a: i = cur
    cur += 1
print(abs(j - i))

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

a = -1
b = -1
for i,v in enumerate(permutations(range(1,N+1), N)):
    if v==p:
        a = i
    if v==q:
        b = i


print(abs(a-b))

Solution (Python):
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)]
ans1=0
k=0
ans2=0
for i in itertools.permutations(a):
    if list(i)==p:
        ans1=k
    if list(i)==q:
        ans2=k
    k+=1
print(abs(ans1-ans2))

Solution (Python):
from itertools import permutations


def solve():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    
    a, b = -1, -1
    cnt = 0
    for nums in permutations(range(1, N + 1), N):
        nums = list(nums)
        if nums == P:
            a = cnt
        if nums == Q:
            b = cnt
        cnt += 1
    print(abs(a - b))


if __name__ == '__main__':
    solve()

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

from itertools import permutations

for i,p in enumerate(list(permutations(range(1,N+1)))):
    flag = True
    for j in range(N):
        if p[j]!=P[j]:
            flag=False
            break
    if flag:
        break
a = i+1

for i,p in enumerate(list(permutations(range(1,N+1)))):
    flag = True
    for j in range(N):
        if p[j]!=Q[j]:
            flag=False
            break
    if flag:
        break

b=i+1

print(abs(a-b))

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok


def main():

    N = ii()
    P = tuple(mis())
    Q = tuple(mis())
    for i, perm in enumerate(permutations(range(1,N+1))):
        if perm == P:
            a = i
        if perm == Q:
            b = i
    print(abs(a-b))




main()

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
import itertools
def ii(): return int(readline())
def mi(): return map(int, readline().rstrip().split())
def li(): return list(readline().rstrip())
def lmi(): return list(map(int, readline().rstrip().split()))
def end(*arg): print(*arg); sys.exit()



def main():
    n = ii()
    P = tuple(lmi())
    Q = tuple(lmi())
    p, q = 0, 0
    a = [i + 1 for i in range(n)]
    for idx, l in enumerate(itertools.permutations(a, n)):
        if P == l:
            p = idx
        if Q == l:
            q = idx
    print(abs(p - q))
    return


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()))
l=list(permutations(range(1,N+1)))
for i in range(len(l)):
    if P==l[i]:
        p=i
for i in range(len(l)):
    if Q==l[i]:
        q=i
print(abs(p-q))

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

W = []

def dfs(checked, L):
    if len(L) == N:
        W.append(L)
        return
    for i in range(1, N+1):
        if not checked[i]:
            checked[i] = True
            dfs(checked, L+[i])
            checked[i] = False
    return

dfs([False]*(N+1), [])


for i, L in enumerate(W):
    if L == P:
        ip = i
    if L == Q:
        iq = i

print(abs(ip-iq))

Solution (Python):
import math

n=int(input())
plist = list(map(int, input().split()))
qlist = list(map(int, input().split()))

klist=[]
klist2=[]
for i in range(n):
    klist.append(i+1)
    klist2.append(i+1)

answer1=[]
answer2=[]


for j in range(n):
    numb=klist.index(plist[j])
    answer1.append(numb)
    klist=klist[:numb]+klist[numb+1:]

for j in range(n):
    numb=klist2.index(qlist[j])
    answer2.append(numb)
    klist2=klist2[:numb]+klist2[numb+1:]




answer3=0
answer4=0

for i in range(n):
    answer3+=math.factorial(n-i-1)*answer1[i]
    answer4+=math.factorial(n-i-1)*answer2[i]

print(abs(answer3-answer4))

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

perms = sorted([p for p in permutations([i+1 for i in range(n)],n)])
print(abs(perms.index(P)-(perms.index(Q))))

Solution (Python):
import itertools

n = int(input().strip())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
p_cnt = 0
q_cnt = 0
for i, e in enumerate(sorted(list(itertools.permutations(p)))):
    if p == e:
        p_cnt = i
    if q == e:
        q_cnt = i
print(abs(p_cnt - q_cnt))

Solution (Python):
def main():
    import sys
    import itertools
    N=int(sys.stdin.readline())
    PL=list(map(int,sys.stdin.readline().split()))
    QL=list(map(int,sys.stdin.readline().split()))

    for i,L in enumerate(itertools.permutations(range(1,N+1))):
        if tuple(L)==tuple(PL):
            a=i
        if tuple(L)==tuple(QL):
            b=i

    print(abs(a-b))
main()

Solution (Python):
from itertools import permutations

N = int(input())
*P, = map(int, input().split())
*Q, = map(int, input().split())
P = tuple(P); Q = tuple(Q)
for i, t in enumerate(permutations(range(1, N+1))):
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a - b))

Solution (Python):
import sys
from itertools import permutations
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

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

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

Solution (Python):
import itertools

n = int(input())
ps = tuple(map(int, input().split()))
qs = tuple(map(int, input().split()))

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

for i, x in enumerate(itertools.permutations(perm)):
    if x == ps:
        a = i
    if x == qs:
        b = i

print(abs(a - b))

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

import itertools

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

T = [list(i) for i in list(itertools.permutations(A, N))]

n1 = T.index(P)
n2 = T.index(Q)
print(abs(n1-n2))

Solution (Python):
N = int(input())
P_list = list(map(int, input().split()))
Q_list = list(map(int, input().split()))

def all_patterns(n, pattern, patterns):
	if len(pattern) == n:
		patterns.append(pattern)
	else:
		for i in range(1, n + 1):
			if not i in pattern:
				all_patterns(n, pattern + [i], patterns)

patterns = []
all_patterns(N, [], patterns)

print(abs(patterns.index(P_list) - patterns.index(Q_list)))

Solution (Python):
import itertools

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

L = [i for i in range(1, N + 1)]
P = tuple(P)
Q = tuple(Q)

i = 1
for e in itertools.permutations(L):

    if e == P:
        a = i
    if e == Q:
        b = i
    i += 1
print(abs(a - b))

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()]
Ps = "".join(map(str, P))
Qs = "".join(map(str, Q))

Dict = []
for w in permutations(range(1, N+1)):
    Dict.append("".join(map(str, w)))

for i, w in enumerate(Dict):
    if w == Ps: a = i
    if w == Qs: b = i
print(abs(a - b))

Solution (Python):
from itertools import permutations

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

for i, pat in enumerate(permutations(range(1, n + 1), n)):
    if pat == p:
        a = i + 1
    if pat == q:
        b = i + 1

print(abs(a - b))

Solution (Python):



import itertools

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n = ri()
    lp = rli()
    lq = rli()
    seq = list(range(1, n+1))
    ll = list(itertools.permutations(seq))
    a = None
    for i, l in enumerate(ll):
        if list(l) == lp:
            a = i
        if list(l) == lq:
            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()))
X = list(range(1, n+1))

pattern = list(itertools.permutations(X))
pattern.sort()

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

print(abs(a-b))

Solution (Python):
from itertools import permutations

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

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

l=[]
l=[aa for aa in permutations(a)]
l=sorted(l)
for i in range(len(l)):
 if l[i]==p:
  pp=i
 if l[i]==q:
  qq=i
print(abs(pp-qq))

Solution (Python):
import itertools

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

print(abs(sorted(list(itertools.permutations(p))).index(p) -
          sorted(list(itertools.permutations(q))).index(q)))

Solution (Python):
from itertools import permutations

n = int(input())
prr = list(map(int, input().split()))
qrr = list(map(int, input().split()))

arr = list(permutations(prr))
arr.sort()
for i in range(len(arr)):
    arr[i] = list(arr[i])

ans = arr.index(prr) - arr.index(qrr)
print(abs(ans))

Solution (Python):
from itertools import permutations
def main():
    N = int( input())
    P = list( map( int, input().split()))
    Q = list( map( int, input().split()))
    Pem = list( map( lambda x: list(x), permutations( range(1, N+1))))

    M = 1
    for i in range(1, N+1):
        M *= i
    for i in range(M):
        if Pem[i] == P:
            p = i
        if Pem[i] == Q:
            q = i
    print( abs(p-q))
if __name__ == '__main__':
    main()

Solution (Python):





from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit










setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
t=list(permutations(range(1,n+1),n))

a,b=0,0
for i in range(len(t)):
    if list(t[i])==p:
        a=i+1
    if list(t[i])==q:
        b=i+1
print(abs(a-b))

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

import itertools

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

lst = list(itertools.permutations(l,n))

a = 0
b = 0

for i in range(len(lst)):
    if lst[i] == p:
        a = i
    if lst[i] == q:
        b = i

print(abs(a-b))

Solution (Python):
import sys
import itertools

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def TI(): return tuple(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n=II()
    p=TI()
    q=TI()
    for i,s in enumerate(itertools.permutations(range(1,n+1))):
        if s==p:a=i
        if s==q:b=i
    print(abs(b-a))
main()

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
data=[]
for ii in itertools.permutations(range(1,n+1),n):
    data.append(ii)
data.sort()
for i,di in enumerate(data):
    if di==P:
        p=i
    if  di ==Q:
        q=i
print(abs(q-p))

Solution (Python):
import itertools

l = []
N = int(input())
*P, = map(int, input().split())
*Q, = map(int, input().split())
P = tuple(P)
Q = tuple(Q)
for i in range(N):
    l.append(i+1)
L = list(itertools.permutations(l))

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

print(abs(a-b))

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

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

a = l.index(p)
b = l.index(q)
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()))
per = [i for i in range(1,N+1)]
per = permutations(per)
count = 0
for i in per:
    count += 1
    if list(i) == P:
        p = count
    if list(i) == Q:
        q = count
print(abs(p-q))

Solution (Python):
import itertools
N = int(input())
P = tuple(input().split())
Q = tuple(input().split())
p = list(itertools.permutations(P))
q = list(itertools.permutations(Q))
p.sort()
q.sort()
for i in range(len(p)):
    if p[i]==P:
        break
for j in range(len(q)):
    if q[j]==Q:
        break
print(abs(i-j))