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):
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

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

a = 0

f = [False] * N
for i in range(N):
    a += (P[i]-1-f[:P[i]-1].count(True)) * fact(N-i-1) + 1
    f[P[i]-1] = True

b = 0

f = [False] * N
for i in range(N):
    b += (Q[i]-1-f[:Q[i]-1].count(True)) * fact(N-i-1) + 1
    f[Q[i]-1] = True

print(abs(a-b))

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

x = [tuple(x) for x in itertools.permutations(range(1, N + 1), N)]
x.sort()
a = x.index(P)
b = x.index(Q)
ans = abs(a - b)
print(ans)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N = I()
P = tuple(LI())
Q = tuple(LI())

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

Solution (Python):
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
for i, t in enumerate(permutations(range(1, N+1))):
    if t == P:
        p = i
    if t == 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()))

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

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


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

a = P.index(p)
b = P.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()))

array=[]

for i in range(1,n+1):
	array.append(i)

z=list(permutations(array,n))

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

print(abs((a+1)-(b+1)))

Solution (Python):
import itertools

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

cnt = 0
p_cnt = 0
q_cnt = 0

for a in itertools.permutations(range(n)): 
    cnt += 1
    for i in range(len(a)):
        if a[i]+1 != p_list[i]:
            break
        if i == len(a)-1:
            p_cnt = cnt
    for j in range(len(a)):
        if a[j]+1 != q_list[j]:
            break
        if j == len(a)-1:
            q_cnt = cnt

print(abs(p_cnt - q_cnt))

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

Solution (Python):
import copy
import math
import time
import statistics
import math
import itertools


def get_int():
    return int(input())

def get_string():
    return input()

def get_int_list():
    return [int(x) for x in input().split()]

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

def get_int_multi():
    return map(int, input().split())

def get_string_char_list():
    return list(str(input()))




'''
while (idx < n) and ():

    idx += 1
'''

def main():
    start = time.time()
    n = get_int()
    p = get_int_list()
    q = get_int_list()

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

    count = 0
    count_p = 0
    count_q = 0
    for keiro in list(itertools.permutations(a)):
        if list(keiro) == p:
            count_p = count
        if list(keiro) == q:
            count_q = count
        count += 1

    print(abs(count_p - count_q))


if __name__ == '__main__':
    main()

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

Solution (Python):

"""
大きさ N の順列 ((1, 2, ..., N) を並び替えてできる数列) P, Qがあります。
大きさ Nの順列は N! 通り考えられます。このうち、P が辞書順で a 番目に小さく、Q が辞書順で b 番目に小さいとして、|a−b|を求めてください。
2つの数列 X, Y について、ある整数 k が存在して Xi=Yi(1≤i<k) かつ Xk<Yk が成り立つとき、X は Y より辞書順で小さいと定義されます。
"""

from itertools import permutations 

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

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

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

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

Solution (Python):
from itertools import permutations

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

s = sorted(permutations(str(P)))
pi = s.index(tuple(str(P)))
qi = s.index(tuple(str(Q)))

print(abs(pi - qi))

Solution (Python):


from itertools import permutations

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

pp = sorted(p)
qq = sorted(q)
P = list(permutations(pp))
Q = list(permutations(qq))


a=P.index(tuple(p))
b=Q.index(tuple(q))
print(abs(a-b))

Solution (Python):
import itertools

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

for j in l:
    if j == P:
        a = l.index(j)+1
    if j == Q:
        b = l.index(j)+1
print(abs(a-b))

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
all_order={}
def dfs(a):
  if len(a)==n:
    all_order[tuple(a)]=len(all_order.keys())+1
  else:
    for nv in range(n):
      nv+=1
      if nv in a:continue
      dfs(a+[nv])
dfs([])
print(abs(all_order[tuple(p)]-all_order[tuple(q)]))

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

def dict_num(ls):
    m = len(ls)
    ans = 0
    already = []
    for i in range(m):
        top = ls[i]
        for j in range(1,top):
            if j not in already:
                ans += math.factorial(m-i-1)
        already.append(top)
    return ans+1
print(abs(dict_num(q_ls)-dict_num(p_ls)))

Solution (Python):
from collections import deque

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

dic = []
que = deque()
for i in range(1, N+1):
    que.append([i])

while que:
    seq = que.popleft()
    if len(seq) == N:
        dic.append(seq)
        continue
    else:
        for i in range(1, N+1):
            if i in seq:
                continue
            seq_next = seq + [i]
            que.append(seq_next)

for i in range(len(dic)):
    if P == dic[i]:
        a = i
    if Q == dic[i]:
        b = i
print(abs(a-b))

Solution (Python):

import itertools

def solve():
    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 = tuple(itertools.permutations(l))
    P_index = p.index(P)
    Q_index = p.index(Q)
    print(abs(Q_index - P_index))


if __name__ == "__main__":
    solve()

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

Solution (Python):
import itertools

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

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

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

Solution (Python):
import itertools

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

count = 1
for p in itertools.permutations(range(1, N+1)):

    if p == P:
        count_P = count
    if p == Q:
        count_Q = count

    count += 1

ans = abs(count_P - count_Q)
print(ans)

Solution (Python):
def getN(P,i):
    n=0
    P1=sorted(P)
    for p in range(i-1):
        n *=(i-p)
        n += (P1.index(P[p]))
        P1.remove(P[p])
    return n

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

p = getN(P,n)
q = getN(Q,n)

print(abs(p-q))

Solution (Python):
import itertools

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

Solution (Python):
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
d = list(itertools.permutations(range(1, n + 1)))
d.sort()

a = -1
b = -1
for i in range(len(d)):
    if d[i] == p:
        a = i
    if d[i] == q:
        b = i
print(abs(a - b))

Solution (Python):









import os
import sys


from itertools import permutations


def main():
    n = int(input())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    permu = list(map(list, permutations(range(1, n+1))))
    for index, per in enumerate(permu):
        if per == p:
            a = index
        if per == q:
            b = index
    print(abs(a-b))


if __name__ == "__main__":
    main()

Solution (Python):
import math


def resolve():
    import sys
    input = sys.stdin.readline
    
    
    n = int(input().rstrip())

    p = tuple(int(x) for x in input().rstrip().split(" "))
    q = tuple(int(x) for x in input().rstrip().split(" "))

    import itertools

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

    for i, v in enumerate(perm):
        if v == p:
            a = i
        if v == q:
            b = i

    print(abs(a-b))



if __name__ == "__main__":
    resolve()

Solution (Python):
import itertools

n = int(input())
a = list()
for i in range(1,n+1):
    a.append(i)
b = list(input().split())
c = list(input().split())
for i in range(0,n):
    b[i]=int(b[i])
    c[i]=int(c[i])
cnt = 1
for i in itertools.permutations(a):
    
    if(tuple(b)==i):
        cnta = cnt
        
    if(tuple(c)==i):
        cntb = cnt
        
    cnt +=1
print(abs(cnta-cntb))

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

l = [int(x) for x in range(1,n+1)]
i = 0
p_i = 0
q_i = 0
for v in itertools.permutations(l, len(l)):
    i += 1
    if p==list(v):
        p_i = i
        
    if q==list(v):
        q_i = i
        
print(abs(p_i-q_i))

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

perm = list(permutations(sorted(p), n))    
a = perm.index(tuple(p))
b = perm.index(tuple(q))

print(abs(a-b))

Solution (Python):
import itertools

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

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

    print(abs(p.index(P) - p.index(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()))
r = 0
for i, x in enumerate(permutations([j for j in range(1,N+1)])):
    if x == P:
        r = abs(r-i)
    if x == Q:
        r = abs(r-i)
print(r)

Solution (Python):
from itertools import *
A = list(permutations(range(1,1+int(input()))))
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 = list(map(int, input().split()))
Q = list(map(int, input().split()))
per = list(itertools.permutations(sorted(P)))

print(abs(per.index(tuple(P)) - per.index(tuple(Q))))

Solution (Python):
import itertools
N=int(input())
ListP = tuple(map(int, input().split()))
ListQ = tuple(map(int, input().split()))
s_l = set(ListP)
Per = list(itertools.permutations(s_l))
Per.sort()
mid1 = 0
mid2 = 0
for i in range(len(Per)):
  if Per[i] == ListP:
    mid1 = i+1
  if Per[i] == ListQ:
    mid2 = i+1
res = abs(mid1 -mid2)
print(res)

Solution (Python):
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
target= range(1,n+1)
p_len=0
q_len=0
for i,v in enumerate(itertools.permutations(target,n)):
    tmp=list(v)
    if p==tmp:
        p_len=i
    if q==tmp:
        q_len=i
print(abs(p_len-q_len))

Solution (Python):
from math import factorial

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

orderP = 1 + (P[0]-1)*factorial(N-1)
orderQ = 1 + (Q[0]-1)*factorial(N-1)
for i in range(1,N):
  redP = 0
  redQ = 0
  for j in range(i):
    if P[j] < P[i]: redP += 1
    if Q[j] < Q[i]: redQ += 1
  orderP += (P[i]-redP-1)*factorial(N-i-1)
  orderQ += (Q[i]-redQ-1)*factorial(N-i-1)
print(abs(orderP - orderQ))

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

T = [''.join(map(str, a)) for a in sorted(list(itertools.permutations(range(1, N + 1))))]

P_i = 0
Q_i = 0
for i, t in enumerate(T):
    if t == P:
        P_i = i
    if t == Q:
        Q_i = i
ans = abs(P_i - Q_i)
print(ans)

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

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

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

Solution (Python):
from itertools import permutations
from bisect import bisect

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

Solution (Python):
import itertools

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

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

cnt = 0
for v in itertools.permutations(l, len(l)):
    if p == list(v):
        pi = cnt
    if q == list(v):
        qi = cnt 
    cnt += 1

print(abs(pi-qi))

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

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

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

i = 0
s = 0
t = 0

for v in itertools.permutations(l):
    
    if v == p:
        s = i
    if v == q:
        t = i
    
    i += 1

print(abs(t - s))

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

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


x = [1] * (N + 1)

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

res1 = 1
res2 = 1

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

for p in P:
    for i in range(len(s1)):
        if p == s1[i]:
            s1.pop(i)
            t = i - 1
            break

    res1 += t * x[len(s1)]

for q in Q:
    for i in range(len(s2)):
        if q == s2[i]:
            s2.pop(i)
            t = i - 1
            break

    res2 += t * x[len(s2)]

print(abs(res1 - res2))

Solution (Python):
import itertools
import numpy as np
import math

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

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

lis = [x+1 for x in range(n)]

p_list = itertools.permutations(lis)

a, b = 0, 0
f_a, f_b = False, False
i = 0

for one_case in p_list:
  if list(one_case) == p:
    a = i
    i += 1
    f_a = True
  elif list(one_case) == q:
    b = i
    i += 1
    f_b = True
  else:
    i += 1
  if f_a and f_b:
    break
print(abs(a-b))

Solution (Python):
import itertools

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

for i, lis in enumerate(itertools.permutations([i for i in range(1, N + 1)])):
  lis = list(lis)
  if lis == P:
    a = i
  if lis == Q:
    b = i

print(abs(a - b))

Solution (Python):
import itertools

n = int(input())
p = list(map(int, input().split()))
q = tuple(map(int, input().split()))
srt = sorted(p.copy())
cnt = 1
cnt1 = 0
cnt2 = 0
for i in itertools.permutations(srt, n):
    if i == tuple(p):
        cnt1 = cnt
    if i == q:
        cnt2 = cnt
    cnt += 1
print(abs(cnt1 - cnt2))

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

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


if __name__=="__main__":
    main()

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = sorted(list(itertools.permutations(range(1,n+1), n)))
    ans = []
    for i in range(len(l)):
        if l[i] == p:
            ans.append(i)
        elif l[i] == q:
            ans.append(i)
    if len(ans) == 1:
        print(0)
    else:
        print(abs(ans[0]-ans[1]))







if __name__=="__main__":
    main()

Solution (Python):
def resolve():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    a, b = None, None
    import itertools
    for i, vars in enumerate(itertools.permutations(range(1, N+1))):
        if a is None:
            for j in range(N):
                if vars[j] != P[j]:
                    break
            else:
                a = i
        if b is None:
            for j in range(N):
                if vars[j] != Q[j]:
                    break
            else:
                b = i
        if a is not None and b is not None:
            break
    print(abs(a-b))


if '__main__' == __name__:
    resolve()

Solution (Python):
import math
def count(N,i,li):
    a = 1
    for t in range(N):
        if li[t] == i:
            return a
        a += 1

N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
li1 = [i+1 for i in range(N)]
li2 = [i+1 for i in range(N)]
x = N-1
a_num = math.factorial(x) * (a[0]-1)
li1.remove(a[0])
b_num = math.factorial(x) * (b[0]-1)
li2.remove(b[0])
x -= 1

for i in range(1,N):
    a_num += math.factorial(x)*(count(N-i,a[i],li1)-1)
    li1.remove(a[i])
    b_num += math.factorial(x)*(count(N-i,b[i],li2)-1)
    li2.remove(b[i])
    x-=1
print(abs((a_num+1)-(b_num+1)))

Solution (Python):
import itertools

n = int(input())

a = list(input().split())
b = list(input().split())
str_a = ''.join(a)
str_b = ''.join(b)
li = [x for x in range(1, n+1)]
map = map(str, li)

p = itertools.permutations(map)
sorted_list = []
for v in p:
    tmp = ''.join(v)
    sorted_list.append(tmp)
sorted_list.sort()

ai = 0
bi = 0
for v in range(len(sorted_list)):
    if str_a == sorted_list[v]:
        ai = v
    elif str_b == sorted_list[v]:
        bi = v
res = abs(ai-bi)
if a == b: res = 0
print(res)

Solution (Python):
import math
import itertools

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

cnt1 = 0
cnt2 = 0
for j in range(math.factorial(N)):
    for i in range(N):
        
        if P[i] == ls2[j][i]:
            cnt1 += 1
        if Q[i] == ls2[j][i]:
            cnt2 += 1
    
    if cnt1 == N:
        a = j
    if cnt2 == N:
        b = j
    cnt1 = 0
    cnt2 = 0

print(abs(a-b))

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

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

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 sys
readline = sys.stdin.readline

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

import itertools
a = 0
b = 0
cnt = 0
for perm in itertools.permutations(range(1, N + 1)):
  cnt += 1
  if perm == P:
    a = cnt
  if perm == Q:
    b = cnt
    
print(abs(a - b))

Solution (Python):
import itertools

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

tmp = [n+1 for n in range(N)]

parms = list(itertools.permutations(tmp, N))

for i in range(len(parms)):
    if parms[i] == P:
        a = i
    if parms[i] == Q:
        b = i
        
print(abs(a-b))

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

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

now=0
for i in l:
  if i==p:
    a=now
  if i==q:
    b=now
  now+=1
print(abs(a-b))

Solution (Python):

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

res = [0, 0]
cnt = [0]


def dfs(x):
    if len(x) == N:
        if all(u == v for u, v in zip(P, x)):
            res[0] = cnt[0]
        if all(u == v for u, v in zip(Q, x)):
            res[1] = cnt[0]
        cnt[0] += 1
        return

    for i in range(1, N + 1):
        if i not in x:
            x.append(i)
            dfs(x)
            x.pop()


dfs([])
print(abs(res[0] - res[1]))

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

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

kai=1

for i in range(n):
    kai*=i+1

for i in range(kai):
    if all(p[w]==g[i][w] for w in range(n)):
        a=i+1
for i in range(kai):
    if all(q[w]==g[i][w] for w in range(n)):
        b=i+1
print(abs(a-b))

Solution (Python):
from itertools import permutations as per
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
PQ = list(per(list(range(1, N + 1))))

a = PQ.index(P) + 1
b = PQ.index(Q) + 1

print(abs(a - b))

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

count=0
l = []
for i in range(1,N+1):
  l.append(i)
for i,v in enumerate(itertools.permutations(l)):
  if p==v:
    a = i+1
  if q==v:
    b=i+1
    
print(abs(a-b))

Solution (Python):
def solve():
    def backtrack(nums, cur_state, permutations):
        if not nums:
            permutations[' '.join(cur_state)] = len(permutations)
        for i in range(len(nums)):
            cur_state.append(nums[i])
            backtrack(nums[:i]+nums[i+1:], cur_state, permutations)
            cur_state.pop()

    num = int(input())
    P = str(input())
    Q = str(input())
    
    nums = [str(i) for i in list(range(1, num+1))]
    permutations = {}
    backtrack(nums, [], permutations)
    
    print(abs(permutations[Q]-permutations[P]))


if __name__ == "__main__":
    solve()

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

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

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

Solution (Python):
import itertools

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

a = 0
b = 0
for i, n in enumerate(itertools.permutations(N)):
    if n == P:
        a = i
    if n == 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(range(1,n+1))
r = list(itertools.permutations(l, n))

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

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

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


x = [1] * (N + 1)

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

res1 = 1
res2 = 1

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

for p in P:
    for i in range(len(s1)):
        if p == s1[i]:
            s1.pop(i)
            t = i - 1
            break

    res1 += t * x[len(s1)]

for q in Q:
    for i in range(len(s2)):
        if q == s2[i]:
            s2.pop(i)
            t = i - 1
            break

    res2 += t * x[len(s2)]

print(abs(res1 - res2))

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

l=[i+1 for i in range(n)]
perm = permutations(l)
perm=list(perm)
p=[]
for i in perm:
    k=(''.join(map(str,i)))
    p.append(int(k))
a=''.join(map(str,a))
b=''.join(map(str,b))
idx1=p.index(int(a))
idx2=p.index(int(b))
print(abs(idx1-idx2))

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


def main():

    input = sys.stdin.readline

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

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


if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

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

a = 0
b = 0
for i in range(len(p_list)):
  if p_list[i] == P:
    a = i+1
  if p_list[i] == Q:
    b = i+1


print(abs(a-b))

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

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

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

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

t=[i for i in range(1,n+1)]
a = list(itertools.permutations(t))




def num(b,t,n):
  c = 0
  for i in range(len(t)):
    if list(t[i]) != b:
      c += 1
    else:
      break
  return(c)


 
x = num(p,a,n)
y = num(q,a,n)
print(abs(x-y))

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

Solution (Python):
import math

NN = int(input())
AA = list(map(int,input().split()))
BB = list(map(int,input().split()))


AA_converted = 0
BB_converted = 0
LL = list(range(1,NN+1))
for ii in range(0,NN):
    AA_converted += math.factorial(NN-1-ii)*LL.index(AA[ii])
    LL.remove(AA[ii])



LL = list(range(1,NN+1))
for ii in range(0,NN):
    BB_converted += math.factorial(NN-1-ii)*LL.index(BB[ii])
    LL.remove(BB[ii])



print(abs(AA_converted-BB_converted))

Solution (Python):
import itertools

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

Psort = sorted(P)
Pp = list(itertools.permutations(Psort))

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

print(abs(a-b))

Solution (Python):
import itertools

n = int(input())
j = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
j_i, q_i = None, None
for i, t in enumerate(itertools.permutations(range(1,n+1), n), start=1):
    if t == j:
        j_i = i
    if t == q:
        q_i = i
    if j_i and q_i:
        break
print(abs(j_i - q_i))

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), n))
print(abs(L.index(P) - L.index(Q)))

Solution (Python):
import itertools

n = int(input())
s = list(map(int, input().split(' ')))
t = list(map(int, input().split(' ')))
hoge = list(itertools.permutations(range(1, n+1)))
p = 0
q = 0
for i in range(len(hoge)):
  if list(hoge[i]) == s:
    p = i
  if list(hoge[i]) == t:
    q = i
print(abs(p - q))

Solution (Python):
from itertools import permutations


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

    A = [i for i in range(1, N+1)]
    As = list(permutations(A))
    for i in range(len(As)):
        a = As[i]
        for j in range(len(a)):
            if a[j] != P[j]:
                break
        else:
            p_rank = i
            break

    for i in range(len(As)):
        a = As[i]
        for j in range(len(a)):
            if a[j] != Q[j]:
                break
        else:
            q_rank = i
            break
    print(abs(p_rank-q_rank))


if __name__ == '__main__':
    main()

Solution (Python):

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


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


a = 0
b = 0
for i, perm in enumerate(perms):
    perm = list(perm)
    if perm == p:
        a = i
    if perm == q:
        b = i

import numpy as np
print(np.abs(a-b))

Solution (Python):
from itertools import permutations

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

seqs = []
for i, x in enumerate(permutations(range(1, N + 1), N)):
    if x == P:
        p = i + 1
    
    if x == Q:
        q = i + 1
print(abs(p-q))