Problem:
 Score : 300 points    Problem Statement We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N) ).  There are N! possible permutations of size N . Among them, let P and Q be the a -th and b -th lexicographically smallest permutations, respectively. Find |a - b| .      Notes For two sequences X and Y , X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k .      Constraints  2 \leq N \leq 8  P and Q are permutations of size N .         Input Input is given from Standard Input in the following format:  N  P_1  P_2  ...  P_N  Q_1  Q_2  ...  Q_N       Output Print |a - b| .        Sample Input 1 3
1 3 2
3 1 2      Sample Output 1 3  There are 6 permutations of size 3 : (1,~2,~3) , (1,~3,~2) , (2,~1,~3) , (2,~3,~1) , (3,~1,~2) , and (3,~2,~1) . Among them, (1,~3,~2) and (3,~1,~2) come 2 -nd and 5 -th in lexicographical order, so the answer is |2 - 5| = 3 .       Sample Input 2 8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1      Sample Output 2 17517       Sample Input 3 3
1 2 3
1 2 3      Sample Output 3 0

Solution (Python):
import itertools

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [i for i in range(1, N+1)]
    l = list(itertools.permutations(l))
    
    a = 0
    for k, v in enumerate(l):
        if A == list(v):
            a = k+1
        
    b = 0
    for k, v in enumerate(l):
        if B == list(v):
            b = k+1
    return abs(a-b)

print(solve())

Solution (Python):
from itertools import permutations


n = int(input())
A = [i for i in range(1, n+1)]
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
if P == Q:
    print(0)
    exit()
cnt = 0
for v in permutations(A, n):
    cnt += 1
    if v == P:
        a = cnt
    elif v == Q:
        b = cnt
print(abs(a - b))

Solution (Python):
def kaijo(num):
    a = 1
    for i in range(num, 1, -1):
        a *= i
    return a

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

p_len = 0
for i in range(n - 1):
    a = p[i]
    p_len += (len(list(filter(lambda x: x < a, p[i:]))) - 1) * kaijo(n - i - 1)

for i in range(n - 1):
    b = q[i]
    p_len -= (len(list(filter(lambda x: x < b, q[i:]))) - 1) * kaijo(n - i - 1)

print(abs(p_len))

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

from itertools import permutations as pe
arr=[list(x) for x in pe(range(1,n+1))]

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

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
  
  
def factorial(n):
  if n==1:
    return 1
  else:
    return n * factorial(n-1)
  
num_p=0
num_q=0
for i in range(n-1):
  sort_p = sorted(p[i:],reverse=True)
  index_p = sort_p.index(p[i])
  mp=n-(index_p+1)
  num_p += mp*factorial(n-(i+1))
  
  
  sort_q = sorted(q[i:],reverse=True)
  index_q = sort_q.index(q[i])
  mq = n-(index_q+1)
  num_q += mq*factorial(n-(i+1))
  
print(abs(num_p-num_q))

Solution (Python):
from math import factorial
n=int(input())
*l1,=map(int,input().split())
*l2,=map(int,input().split())
ansl=[]
for l in [l1,l2]:
  l_ref = list(range(1,n+1))
  ans = 0
  m = n-1
  for i in l:
    ans += factorial(m) * l_ref.index(i)
    l_ref.remove(i)
    m-=1
  ansl.append(ans)
print(abs(ansl[1]-ansl[0]))

Solution (Python):
input()
a=list(map(int,input().split()))
b=list(map(int,input().split()))
fa=lambda n:1 if n==0 else n*fa(n-1)
in1=1
in2=1
li=[i+1 for i in range(len(a))]
li2=[i+1 for i in range(len(a))]
for i in a:
  in1+=fa(len(li)-1)*li.index(i)
  del li[li.index(i)]
for i in b:
  in2+=fa(len(li2)-1)*li2.index(i)
  del li2[li2.index(i)]
print(abs(in1-in2))

Solution (Python):


import itertools

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

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

a = 0
b = 0

for i, v in enumerate(X):
    if v == P:
        a = i
    if v == Q:
        b = i

print(abs(a-b))

Solution (Python):
N = int(input())
P = "".join(input().split())
Q = "".join(input().split())
l_p = []

def f_perm(bit, s_p):
  if len(s_p) == N:
    l_p.append(s_p)
    return
  for i in range(N):
    if 1 << i & bit != 0:
      continue
    f_perm(bit | 1 << i, s_p + str(i + 1))

f_perm(0, "")    
    
print(abs(l_p.index(P) - l_p.index(Q)))

Solution (Python):
import itertools
N=int(input())
a="".join(input().split())
b="".join(input().split())
li=[]
for k in itertools.permutations([str(i+1) for i in range(N)],N):
    li.append("".join(k))
li.sort()
print(abs(li.index(a)-li.index(b)))

Solution (Python):
import sys

import math
import itertools

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

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

Solution (Python):
import sys
from itertools import permutations 
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

arr = []
perm = permutations(sorted(Q))
for x in perm:
   arr.append("".join(str(y) for y in x))

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

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

a = 0
b = 0
for i,now in enumerate(itertools.permutations(range(n))):
  if (now == p):
    a = i
  if (now == q):
    b = i
print(abs(a-b))

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

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

a = sum([1 if list(p)<=P else 0 for p in perm])
b = sum([1 if list(p)<=Q else 0 for p in perm])

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,b=0,0

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

Solution (Python):
import itertools


def checkArrays(arr1, arr2):
    if len(arr1) != len(arr2):
        return False

    for i in range(len(arr1)):
        if arr1[i] != arr2[i]:
            return False

    return True


n = int(input())

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

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

a = b = 1
for i in range(len(arrays)):
    ar = list(arrays[i])
    if checkArrays(ar, p):
        a = i + 1
    if checkArrays(ar, q):
        b = i + 1

print(abs(a - b))

Solution (Python):

from itertools import permutations


def main():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    perms = tuple(permutations(range(1, N + 1)))
    ans = abs(perms.index(P) - perms.index(Q))
    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
from itertools import permutations

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

l=[_ for _ in range(1,n+1)]
x=list(permutations(l, len(p)))
y=x.index(tuple(p))
z=x.index(tuple(q))
print(abs(y-z))

Solution (Python):
from itertools import combinations_with_replacement as cwr , combinations , permutations
n=int(input())
a=tuple([int(i) for i in input().split()])
b=tuple([int(i) for i in input().split()])
ans=list(permutations(list(range(1,n+1))))
print(abs(ans.index(a)-ans.index(b)))

Solution (Python):
import itertools

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

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

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

p_n = func1(n_list, p)
q_n = func1(n_list, q)

print(abs(p_n[0] - q_n[0]))

Solution (Python):
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
P = LIST()
Q = LIST()

import itertools

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

print(abs(a-b))

Solution (Python):

from itertools import permutations
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
a, b = 0, 0
for i, x in enumerate(permutations(range(1, n + 1))):
    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()))

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

ans = abs(a - b)

print(ans)

Solution (Python):
from itertools import permutations

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

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

    if pat == q:
        b = i

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

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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
import itertools
N = int(input())
P = tuple(map(lambda x: int(x)-1, input().split()))
Q = tuple(map(lambda x: int(x)-1, input().split()))
permu_list = itertools.permutations(range(N))
p,q = 0,0
c = 0
for l in sorted(permu_list):
  if l == P:
    p = c
  if l == Q:
    q = c
  c += 1
print(abs(p-q))

Solution (Python):
n=int(input())
P=[int(i) for i in input().split()]
Q=[int(i) for i in input().split()]
import itertools
ct=1
for c in itertools.permutations(range(n),n):
  c=list(c)
  for i in range(n):
    if P[i]-1==c[i]:
      continue
    else:
      break
  else:
    a=ct
  for i in range(n):
    if Q[i]-1==c[i]:
      continue
    else:
      break
  else:
    b=ct
  ct+=1
print(abs(a-b))

Solution (Python):
from itertools import permutations

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

X = [tuple(p) for p in permutations(range(1, N + 1), r=N)]
X.sort()

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

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


def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    c1 = 0
    for per in permutations(range(1, n+1)):
        c1 += 1
        if per == p:
            break
    c2 = 0
    for per in permutations(range(1, n+1)):
        c2 += 1
        if per == q:
            break
    print(abs(c1-c2))


if __name__ == "__main__":
    main()

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

Solution (Python):
import math
import copy
from copy import deepcopy
import sys
import fractions

from functools import reduce

import heapq
import collections
import itertools
sys.setrecursionlimit(100001)






def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList




n = int(input())

data = []
for i in range(2):
    input_data = input().split()
    tmp = [int(n) for n in input_data]
    data.append(tuple(tmp))

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

index_d0 = -1
index_d1 = -1

for i in range(len(perm)):
    if perm[i] == data[0]:
        index_d0 = i+1
    if perm[i] == data[1]:
        index_d1 = i+1
    if index_d0 > -1 and index_d1 > -1:
        break

smaller_num = min(index_d0, index_d1)
larger_num = max(index_d0, index_d1)

ans = abs(smaller_num - larger_num)
print(ans)

Solution (Python):
from itertools import permutations

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

iP, iQ = None, None
for idx, perm in enumerate(permutations(range(1, N + 1))):
    if perm == P:
        iP = idx
    if perm == Q:
        iQ = idx
print(abs(iP - iQ))

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

s = [0]*(n+1)
s[1] = 1
for i in range(2, n+1):
  s[i] = s[i-1]*i

x = 0
l = [i for i in range(1, n+1)]
for i in p:
  x += l.index(i) * s[len(l) - 1]
  l.remove(i)

y = 0
l = [i for i in range(1, n+1)]
for i in q:
  y += l.index(i) * s[len(l) - 1]
  l.remove(i)
  
print(abs(x-y))

Solution (Python):
from itertools import permutations
N = int(input())
n = list(range(1, N+1))
a_tuple = tuple(map(int, input().split()))
b_tuple = tuple(map(int, input().split()))
a = 0
b = 0
for i, tup in enumerate(sorted(list(permutations(n))), 1):
    if tup == a_tuple:
        a = i
    if tup == b_tuple:
        b = i
print(abs(a - b))

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

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

a,b = -1,-1
for i,x in enumerate(permutations([i+1 for i in range(n)])):
    c,d = True,True
    for j in range(n):
        if x[j] != p[j]:
            c = False
        if x[j] != q[j]:
            d = False
    if c:a = i+1
    if d:b = i+1
print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
p = list(map(int, input().split()))
a  = sorted(p)
q = list(map(int, input().split()))
cntp = 0
cntq = 0
cnt=0
for i in itertools.permutations(a):
  cnt+=1
  if p==list(i):
    cntp=cnt
  if q==list(i):
    cntq = cnt
print(abs(cntp-cntq))

Solution (Python):
import itertools
n = int(input())
p = [int(x) for x in input().split()]
a  = sorted(p)
q = [int(x) for x in input().split()]
cntp = 0
cntq = 0
cnt=0
for i in itertools.permutations(a):
  cnt+=1
  if p==list(i):
    cntp=cnt
  if q==list(i):
    cntq = cnt
print(abs(cntp-cntq))

Solution (Python):
import itertools

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

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

p = 0
q = 0
for i in range(len(l)):
    if P == list(l[i]):
        p = i
    if Q == list(l[i]):
        q = i
        
print(abs(p-q))

Solution (Python):
import math
from collections import deque
def main():
    n = int(input())
    p = [int(t)for t in input().split()]
    q = [int(t)for t in input().split()]

    p_num = 0
    q_num = 0

    fact = [1]*n

    for i in range(n):
        fact[i] = fact[i-1]*(i+1)

    fact = fact[::-1]
    fact.append(1)

    temp = set([])
    for i, p_ in enumerate(p):
        t = p_-len([x for x in temp if x<p_])-1
        p_num += fact[i+1]*t
        temp.add(p_)

    temp = set([])
    for i, q_ in enumerate(q):
        t = q_-len([x for x in temp if x<q_])-1
        q_num += fact[i+1]*t
        temp.add(q_)

    print(abs(p_num-q_num))

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()))
li = [i+1 for i in range(N)]
for i, v in enumerate(permutations(li, N)):
    if v == p: a = i
    if v == q: b = i
print(abs(a-b))

Solution (Python):
from itertools import permutations

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

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

Solution (Python):
import math
import itertools
N = int(input())
nums = []
for i in range(1, N + 1):
    nums.append(i)
permutaions_list = list(itertools.permutations(nums, N))
Ps = tuple(map(int, input().split()))
Qs = tuple(map(int, input().split()))

print(abs(permutaions_list.index(Ps) - permutaions_list.index(Qs)))

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

Solution (Python):
import itertools

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

permutations = sorted(P)
count = 0
p_index = 0
q_index = 0
for i in itertools.permutations(permutations,N):
    count+=1
    if "".join((str(j) for j in i)) == "".join([str(i) for i in P]):
        p_index = count
    if "".join((str(j) for j in i)) == "".join([str(i) for i in Q]):
        q_index = count
print(abs(p_index-q_index))

Solution (Python):
from math import factorial
n = int(input())

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

numlist = list(range(1,n+1))
for i in range(n):
    P_order += numlist.index(P[i]) * factorial(n-i-1)
    numlist.remove(P[i])

numlist = list(range(1,n+1))
for i in range(n):
    Q_order += numlist.index(Q[i]) * factorial(n-i-1)
    numlist.remove(Q[i])

print(abs(P_order - Q_order))

Solution (Python):
import itertools
n = int(input())
a = tuple([int(i) for i in input().split()])
b = tuple([int(i) for i in input().split()])

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

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

Solution (Python):
import itertools
n = int(input())
a = tuple([int(i) for i in input().split()])
b = tuple([int(i) for i in input().split()])

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

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

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

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

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

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

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

cnt = 1
for pattern in pattern_list:
    if pattern == p_list:
        a = cnt
    if pattern == q_list:
        b = cnt
    cnt+=1


print(abs(a-b))

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

Solution (Python):
import itertools
n = int(input())
P = int(input().replace(' ', ''))
Q = int(input().replace(' ', ''))
N = [i + 1 for i in range(n)]
per = list(itertools.permutations(N))
lis = []
for oc in per:
    lis.append(int(''.join(map(str, list(oc)))))
a = lis.index(P)
b = lis.index(Q)
print(abs(a - b))

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
cnt=0
from  itertools import permutations
for i in permutations(range(1,1+n)):
    cnt+=1
    i=list(i)
    if     i==p:
        a=cnt
        break
cnt=0
for i in permutations(range(1,1+n)):
    cnt+=1
    i=list(i)
    if     i == q:
        b=cnt
        break


print(abs(a-b))

Solution (Python):
import sys
from itertools import permutations
int1 = lambda x: int(x) - 1

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
 
n = int(readline())
p = tuple(map(int1, readline().split()))
q = tuple(map(int1, readline().split()))
 
l = list(permutations(list(range(n))))
 
print(abs(l.index(q) - l.index(p)))

Solution (Python):
import sys
import os
import itertools


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

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

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

    allcase = list(itertools.permutations(seq))
    ret = []
    for i, case in enumerate(allcase):
        if case == P:
            ret.append(i)
        if case == Q:
            ret.append(i)

    print(abs(ret[0] - ret[1]))


if __name__ == '__main__':
    main()

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

def kaijou(x):
    result = 1
    for i in range(1, x+1):
        result *= i
    return result

def nokori(X, y):

    X = sorted(X)
    idx = 0
    for i in X:
        if i == y:
            return idx
        idx += 1
a = 1
tmp_P = P.copy()
for idx, i in enumerate(P):
    if idx < len(P):
        a = a + (nokori(tmp_P,i))*kaijou(N-idx-1)
        tmp_P.remove(i)
b = 1
tmp_Q = Q.copy()
for idx, i in enumerate(Q):
    if idx < len(Q):
        b += (nokori(tmp_Q,i))*kaijou(N-idx-1)
        tmp_Q.remove(i)
if a > b:
    print(a-b)
else:
    print(b-a)

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

def counter(p):
    c = 0
    used = [0 for _ in range(N)]
    for i in range(N):
        used[p[i] - 1] = 1
        for k in range(1, p[i]):
            if used[k - 1] != 1:
                c += math.factorial(N - (i + 1))
    
    return c

print(abs(counter(p) - counter(q)))

Solution (Python):
import itertools

N = int(input())
P = int(input().replace(' ', ''))
Q = int(input().replace(' ', ''))
l = [i for i in range(1, N+1)]
pl = itertools.permutations(l)
lis = []
for oc in pl:
  lis.append(int(''.join(map(str, list(oc)))))
lis.sort()
a = lis.index(P)
b = lis.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()))
p_th = 0
for per in itertools.permutations(range(1, 1+N)):
    p_th += 1
    if P == list(per):
        break

q_th = 0
for per in itertools.permutations(range(1, 1+N)):
    q_th += 1
    if Q == list(per):
        break

print(abs(p_th - q_th))

Solution (Python):
import itertools
def listtoint(l, n):
    s = 0
    for i in range(n):
        s = s + l[i]*10**(n-1-i)
    return s
def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    n = []
    for v in itertools.permutations(P):
        s = 0
        for i in range(N):
            s = s + v[i]*10**(N-1-i)
        n.append(s)
    p = listtoint(P, N)
    q = listtoint(Q, N)
    a, b = 0, 0
    for num in n:
        if num < p:
            a = a + 1
        if num < q:
            b = b + 1
    print(abs(a-b))
main()

Solution (Python):
import sys
import itertools
 
INF = 1001001001
 
def resolve():
  n = int(input())
  p = list(map(int, input().split()))
  q = list(map(int, input().split()))
  arr = []
  for i in range(1, n+1):
    arr.append(i)
 
  x = itertools.permutations(arr, n)
 
  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1
 
  print(abs(a - b))
resolve()

Solution (Python):
import itertools

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

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

    p = list(itertools.permutations(N_range,N))
    
    

    start = 0
    end = 0

    for i in range(len(p)):
        if(list(p[i]) == P):
            start = i
        if(list(p[i]) == Q):
            end = i

    if(start > end):
        print(start - end)
    else:
        print(end - start)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import itertools

INF = 1001001001

def resolve():
  n = int(input())
  p = list(map(int, input().split()))
  q = list(map(int, input().split()))
  arr = []
  for i in range(1, n+1):
    arr.append(i)

  x = itertools.permutations(arr, n)

  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1

  print(abs(a - b))
resolve()

Solution (Python):
def add_num(cur,fut):
  for x in fut:
    ne = cur + str(x)
    rep = fut[:]
    rep.remove(x)
    
    if len(rep) > 0:
      add_num(ne,rep)
    else:
      p_list.append(int(ne))

  
  return p_list

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

plist = ''
qlist = ''

for p in P:
  plist = plist + p

for q in Q:
  qlist = qlist + q
  
pl = int(plist)
ql = int(qlist)


p_list = []
fut = [i for i in range(1,N+1)]
cur = ''

add_num(cur,fut)

p_list.sort()



a = p_list.index(pl)
b = p_list.index(ql)

if a-b < 0:
  print(b-a)
else :
  print(a-b)

Solution (Python):
import sys
import itertools

INF = 1001001001

def resolve():
  n = int(input())
  p = list(map(int, input().split()))
  q = list(map(int, input().split()))
  arr = []
  for i in range(1, n+1):
    arr.append(i)

  x = itertools.permutations(arr, n)

  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1

  print(abs(a - b))

  return

if __name__ == "__main__":
    resolve()

Solution (Python):
from itertools import permutations
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
l=list(permutations([i for i in range(1,n+1)]))
a=0
b=0
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):
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
d={}
import itertools
l=list(itertools.permutations(range(1,n+1)))
l.sort()
for i,k in enumerate(l):
    d[k]=i

print(abs(d[p]-d[q]))

Solution (Python):
import itertools






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

candidates = list(itertools.permutations(range(1,N+1)))
a = candidates.index(P)
b = candidates.index(Q)

print(abs(a - b))

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


def orders(l, n):
    ini = 0
    s = [int (i) for i in range(1, n+1)]
    for number in l:
        order = s.index(number)
        s.remove(number)
        l_num = math.factorial(len(s)) * order
        ini = ini + l_num
    return ini

p_place = orders(p, n)
q_place = orders(q,n)

result = abs(p_place - q_place)
print(result)

Solution (Python):
from itertools import permutations

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

Solution (Python):
import sys
from io import StringIO
import unittest

def kaiseki(nums,res=[],s=""):
    for i in range(len(nums)):
        tmp = nums[:]
        a = str(tmp.pop(i))
        if len(tmp) > 0:
            kaiseki(tmp,res,s+a)
        else:
            res.append(int(s+a))

def resolve():
    readline=sys.stdin.readline

    n=int(readline().strip())
    p = list(map(str,readline().strip().split()))
    q = list(map(str,readline().strip().split()))

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

    nums=list(range(1,n+1))
    res=[]
    kaiseki(nums,res,"")
    res.sort()

    i=0
    for r in res:
        if r==p:
            p=i
        if r==q:
            q=i
        i+=1

    diff=p-q
    if diff < 0:
        diff *= -1
    
    print(diff)

    return

resolve()

Solution (Python):
def main():
  import sys
  import itertools
  input = sys.stdin.readline
  n=int(input())
  p=int(''.join(input().split()))
  q=int(''.join(input().split()))
  p,q=max(p,q),min(p,q)
  a,b,l=0,0,[str(i) for i in range(1,n+1)]
  for i in itertools.permutations(l):
    i=int(''.join(i))
    if q<i and i<=p:
      a+=1
    if p<i:
      break
  print(a)  
if __name__ == '__main__':
    main()

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


from itertools import permutations
per = list(permutations(seq,n))





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

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

Solution (Python):
import itertools 

def minus(n):
    return int(n)-1

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

list = list(itertools.permutations(range(num)))

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

Solution (Python):
def permutations(L):
    if L == []:
        return [[]]
    else:
        return [[h] + t for i, h in enumerate(L) for t in permutations(L[:i] + L[i+1:])]


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

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

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

print(abs(a - b))