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 collections import deque

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

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

n_list = []
que = deque([[0,[i+1 for i in range(n)]]])
num = 0
temp_list = []

while len(que) != 0:
    temp = que.popleft()
    if len(temp[1]) == 0:
        n_list.append(temp[0])
        continue
    for i in range(len(temp[1])):
        num = 0
        temp_list = []
        for j in range(len(temp[1])):
            temp_list.append(temp[1][j])
        num += temp_list[i] + temp[0]*10
        temp_list.pop(i)
        que.append([num,temp_list])

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

print(abs(b-a))

Solution (Python):
from itertools import permutations

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

s=list(permutations(nn))

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

print(abs(a-b))

Solution (Python):
import math
import itertools
N = int(input())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
a = []
for i in range(1,N+1):
    a.append(i)
junn = list(itertools.permutations(a))
flag = 0
for i in range(math.factorial(N)):
    if p == list(junn[i]):
        pp = i
        if flag == 1:
            break
        flag = 1
    if q == list(junn[i]):
        qq = i
        if flag == 1:
            break
        flag = 1
print(abs(pp-qq))

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):
def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  N=len(x)
  for i in range (N):
    k=0
    for j in range(i,N):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(N-i-1)
  return result

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

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

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

def main():
    n = int(input())
    p = tuple([int(i) for i in input().split()])
    q = tuple([int(i) for i in input().split()])
    seq = tuple(range(1, n+1))
    num_p = 0
    num_q = 0
    cnt = 0
    for i in itertools.permutations(seq):
        cnt += 1
        if i == p:
            num_p = cnt
        if i == q:
            num_q = cnt
    print(abs(num_p-num_q))

if __name__ == '__main__':
    main()

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

a=-1
b=-1
for i,case in enumerate(itertools.permutations(range(n))):
  if all([1 if p[i]==case[i] else 0 for i in range(n)]):
    a=i
  if all([1 if q[i]==case[i] else 0 for i in range(n)]):
    b=i
  if a >= 0 and b >= 0:
    break
print(abs(a-b))

Solution (Python):
N = int(input())
p1 = tuple(map(int,input().split()))
p2 = tuple(map(int,input().split()))
def dfs(pair=[]):
  global lst
  if len(pair) == N:
    lst.append(tuple(pair))
  else:
    for j in range(1,N+1):
      if j not in pair:
        pair.append(j)
        dfs(pair)
        pair.pop()

lst = []
dfs()
lst.sort()
print(abs(lst.index(p1) - lst.index(p2)))

Solution (Python):
import itertools
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


n = I()
p = tuple(LS())
q = tuple(LS())

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

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

import itertools
seq = range(1,n+1)
dic=list(itertools.permutations(seq))
a=dic.index(p)
b=dic.index(q)
print(abs(a-b))

Solution (Python):
import math

def f(X):
    N = len(X)
    ret = 0  
    for i in range(N):
        k = 0
        for j in range(i, N):
            if X[i] > X[j]:
                k += 1
        ret += k * math.factorial(N-i-1)
    return ret

N = int(input())

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

a = f(P)
b = f(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_int = "".join(map(str, P))
Q_int = "".join(map(str, Q))

l = range(1, N+1)
p = itertools.permutations(l)
c_p = 0
c_q = 0
for ele in p:
    if "".join(map(str, ele)) < P_int:
        c_p += 1
    if "".join(map(str, ele)) < Q_int:
        c_q += 1
print(abs(c_p-c_q))

Solution (Python):
import itertools

n=int(input())
l=list(range(1, n+1))
ptn = sorted(list(itertools.permutations(l)))
p=tuple(map(int, input().split()))
q=tuple(map(int, input().split()))

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

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

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

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

Solution (Python):
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
p = tuple(map(int,readline().split()))
q = tuple(map(int,readline().split()))
if p == q:
  print(0)
  sys.exit()
l = [i+1 for i in range(N)]
count = 1
a=[]
for v in itertools.permutations(l):
  count += 1
  if v== p or v ==q:
    a.append(count)
print(a[1]-a[0])

Solution (Python):
import itertools 



N=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))
before=0
after=0
count=0

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


D=list(itertools.permutations(T))
for Z in D:
    count+=1
    if A==Z:
        
        before=count
    if B==Z:
       
        after=count
    
print(abs(before-after))

Solution (Python):
import itertools
 
N = int(input())
p_list = tuple(map(int, input().split()))
q_list = tuple(map(int, input().split()))
 
permu_list = list(itertools.permutations(range(1,N+1)))
p_index = permu_list.index(p_list)
q_index = permu_list.index(q_list)
ans = abs(p_index - q_index)
print(ans)

Solution (Python):
def abc150_c_count_order_2():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    factorial = [1]
    for i in range(1, N+1):
        factorial.append(factorial[i-1] * i)

    def find_index(permutation, lst):
        indices = []
        for p in permutation:
            i = lst.index(p)
            indices.append(i)
            lst.pop(i)
        
        idx = 0
        for i in range(N):
            idx += factorial[N - i - 1] * (indices[i])
        
        return idx

    a = find_index(P, list(range(1, N+1)))
    b = find_index(Q, list(range(1, N+1)))
    print(abs(a-b))



if __name__ == "__main__":
    abc150_c_count_order_2()

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

    def solve(items, current):
        if items == []:
            permutations.append(current)
            return
        for item in items:
            next = current + [item]
            new_items = [i for i in items if i != item]
            solve(new_items, next)

    permutations = []
    items = list(range(1, N+1))
    current = []
    solve(items, current)

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



if __name__ == "__main__":
    abc150_c_count_order()

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

def number(A):
  s=[0]*N
  s[0]=1
  for n in range(1,N):
    s[n]=s[n-1]*(n)
  s[0]=0
  s.reverse()
  t=[i for i in range(N+1)]
  count=0
  for i in range(N):
    count+=s[i]*(t.index(A[i])-1)
    t.pop(t.index(A[i]))
  return count
print(abs(number(q)-number(p)))

Solution (Python):
import itertools as it
N = int(input())
Q = []
P = []
Q = list(map(int, input().split()))
P = list(map(int, input().split()))
Jun = []
box = [i+1 for i in range(N)]

for num in it.permutations(Q):
    num = list(num)
    Jun.append(num)
Jun = sorted(Jun)
for i, num in enumerate(Jun):
    if Q == num:
        a = i
    if P == num:
        b = i
print(abs(a-b))

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

    return
resolve()

Solution (Python):
from itertools import permutations
 
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
 
p = 0
q = 0
i = 0
for n in permutations(range(1,N+1)):
    if n == P:
        p = i
    if n == Q:
        q = i
    i += 1
 
print(abs(p-q))

Solution (Python):
from itertools import permutations

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

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

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

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

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

for i in permutations(range(1, N + 1)):
    a += 1
    b += 1

    if i == P:
        ans = abs(ans - a)

    if i == Q:
        ans = abs(ans - b)

print(ans)

Solution (Python):
import itertools

N = int(input())
P = list(input().split())
Q = list(input().split())
p = itertools.permutations(P)

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

a = 0
b = 0
for v in p:
    if P >= int(''.join(v)): a += 1
    if Q >= int(''.join(v)): b += 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()))

perms = list(permutations([i for i in range(1, N+1)]))

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

print(abs(a - b))

Solution (Python):
import itertools

N = int(input())
p_list = tuple(map(int, input().split()))
q_list = tuple(map(int, input().split()))

permu_list = list(itertools.permutations(range(1,N+1)))
p_index = permu_list.index(p_list)
q_index = permu_list.index(q_list)
ans = abs(p_index - q_index)
print(ans)

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fac = [0] * 8

fac[1] = 1
for i in range(2, N):
    
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))


for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[i])

print(abs(a - b))

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

import itertools
b = 0
c = 0
cnt = 1
for i in itertools.permutations(m,n):
    if i == p:
        c += cnt
    if i == q:
        b += cnt
    if c != 0 and b!=0:
        break
    else:
        cnt += 1


print(abs(c-b))

Solution (Python):
n = int(input())
p = input()
q = input()

import itertools
x = [str(i) for i in range(1,n+1)]

y = []
for i in itertools.permutations(x):
    y.append(''.join(i))

p = list(p)
q = list(q)
p = [a for a in p if a != ' ']
q = [a for a in q if a != ' ']
p = ''.join(p)
q = ''.join(q)

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

Solution (Python):
import itertools

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


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

x = [j for j in itertools.permutations(l)]

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

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

a = 0
b = 0
for i in range(len(perms)):
  if P == list(perms[i]):
    a = i+1
  if Q == list(perms[i]):
    b = i+1
  if a != 0 and b != 0:
    break


print(abs(a-b))

Solution (Python):
import itertools

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
data = list(itertools.permutations(range(1,n+1)))
print(abs(data.index(p)-data.index(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 = list(map(int,input().split()))
Q = list(map(int,input().split()))
L = sorted(list(itertools.permutations(P)))

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

Solution (Python):
import itertools
import math
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
lis = [[] for _ in range(math.factorial(N))]
l = list(range(1,N+1))
lis = list(itertools.permutations(l,N))
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=1
b=1

import math

def dfs(i):
    global a
    global b
    if i==n-1:
        return abs(a-b)

    else:
        N=math.factorial(n-i)

        p_list=p[:]
        q_list=q[:]
        del p_list[0:i]
        del q_list[0:i]
        p_list.sort()
        q_list.sort()
        for j in range(n-i):
            if p[i]==p_list[j]:
                a+=j*N/(n-i)
            if q[i]==q_list[j]:
                b+=j*(N/(n-i))
        dfs(i+1)
dfs(0)
print(abs(int(a-b)))

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

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




Pindex = d_list.index(Ps)
Qindex = d_list.index(Qs)


print(abs(Pindex - Qindex))

Solution (Python):
import itertools
n=int(input())
p = list(map(int,input().split()))	
q = list(map(int,input().split()))
i=1
for lis in list(itertools.permutations(list(range(1,n+1)))):
  if list(lis)==p:
    pin=i
  if list(lis)==q:
    qin=i
  i+=1

print(abs(pin-qin))

Solution (Python):
import itertools

N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
for i in range(N):
    P[i] -= 1
    Q[i] -= 1

R = list(itertools.permutations(range(N)))
length = len(R)
for i in range(length):
    r = list(R[i])
    if P == r:
        a = i
    if Q == r:
        b = i
print(abs(a - b))

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

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

Solution (Python):
import itertools
num = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

lista=[]

for i in range(1,num+1): lista.append(i)

tudo=list(itertools.permutations(lista))

a,b,cont=0,0,0

for i in tudo:
    if list(i) == p: a=cont
    if list(i) == q: b=cont
    cont+=1
print(abs(a-b))

Solution (Python):
from itertools import permutations

n = int(input())
p1 = [int(x) for x in input().split()]
p2 = [int(x) for x in input().split()]
p1 = tuple(p1)
p2 = tuple(p2)
first = [x for x in range(1, n+1)]

for i, p in enumerate(permutations(first)):
    if p == p1:
        a = i
    if p == p2:
        b = i

print(abs(a-b))

Solution (Python):


import itertools

N=int(input())
Ps=tuple(map(int,input().split()))
Qs=tuple(map(int,input().split()))

l=[i+1 for i in range(N)]
cnt=0
Pno=0
Qno=0
for i in itertools.permutations(l):
    if i==Ps:
        Pno=cnt
    if i==Qs:
        Qno=cnt
    cnt+=1
    
print(abs(Qno-Pno))

Solution (Python):
import itertools, math


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


R = list(itertools.permutations(P))
R.sort()
a = 0
b = 0

for i in range(len(R)):
  if list(R[i]) == P:
    a = i + 1
  if list(R[i]) == Q:
    b = i + 1
  if a != 0 and b != 0:
    break


print(abs(b-a))

Solution (Python):
import itertools

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

a = permutation.index(P) + 1
b = permutation.index(Q) + 1
print(abs(a - b))

Solution (Python):
import itertools
N=int(input())
X=[str(i) for i in range(1,N+1)]

s1="".join(input().split())
s2="".join(input().split())
perm=list(map("".join,itertools.permutations(X)))
print(abs(perm.index(s1)-perm.index(s2)))

Solution (Python):
import itertools
N=int(input())
X=[str(i) for i in range(1,N+1)]

s1="".join(input().split())
s2="".join(input().split())
perm=list(map("".join,itertools.permutations(X)))
print(abs(perm.index(s1)-perm.index(s2)))

Solution (Python):
import itertools

n = int(input())

p = "".join(input().split())
q = "".join(input().split())

lst = []

for v in itertools.permutations(map(str, range(1, n+1)), n):
    lst.append("".join(v))

lst.sort()

d = {lst[i]: i for i in range(len(lst))}

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

Solution (Python):
import itertools

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

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

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

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

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

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

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

v = list(itertools.permutations(l))

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

Solution (Python):
import itertools

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

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

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

Solution (Python):
import itertools

n = int(input())

order = list(itertools.permutations([int(x)for x in range(1,n+1)]))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

for i in range(len(order)):
    if P == list(order[i]):
        P_num = i
    if Q == list(order[i]):
        Q_num = i
        
print(abs(P_num-Q_num))

Solution (Python):
import itertools
N=int(input())
P,Q=[tuple(map(int,input().split())) for i in range(2)]
dictionary=sorted(itertools.permutations(P,N))
print(abs(dictionary.index(P)-dictionary.index(Q)))

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

np = [i for i in range(1, n+1)]
p = list(permutations(np))
ai = p.index(a)
bi = p.index(b)
print(abs(ai-bi))

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

np = [i for i in range(1, n+1)]
p = list(permutations(np))
ai = p.index(a)
bi = p.index(b)
print(abs(ai-bi))

Solution (Python):
N=int(input())
*P,=map(int,input().split())
*Q,=map(int,input().split())
nl=[i for i in range(1,N+1)]

import itertools
per=list(itertools.permutations(nl,N))

PP=per.index(tuple(P))+1
QQ=per.index(tuple(Q))+1
print(abs(PP-QQ))

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


M = list(itertools.permutations(L))


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

Solution (Python):
import itertools

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

if P == Q:
    print(0)

else:
    M = [i for i in range(1, N+1)]
    X = list(itertools.permutations(M, N))

    for i in X:
        if list(i) == P:
            a = X.index(i) + 1
        if list(i) == Q:
            b = X.index(i) + 1

    print(abs(a - b))

Solution (Python):

import itertools 
n = int(input())
lnp = tuple(map(int, input().split()))
lnq = tuple(map(int, input().split()))
ln = list(itertools.permutations(list(range(1,n+1))))
np, nq = (0,0)
for i in range(len(ln)) :
    if lnp == ln[i]:
        np = i
        
        break
for i in range(len(ln)) :
    if lnq == ln[i]:
        nq = i
        
        break
        
print(abs(np - nq))

Solution (Python):
n = int(input())
P = [0] + [int(i) for i in input().split()]
Q = [0] + [int(i) for i in input().split()]
L = [0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

Pth = 1
for i in range(1, n+1):
    cnt = 0
    for j in range(i+1, n+1):
        if P[i] > P[j]:
            cnt += 1
    Pth += cnt * L[n - i]


Qth = 1
for i in range(1, n+1):
    cnt = 0
    for j in range(i+1, n+1):
        if Q[i] > Q[j]:
            cnt += 1
    Qth += cnt * L[n - i]


print(abs(Pth-Qth))

Solution (Python):
import itertools

N = int(input())
P = tuple(input().split())
Q = tuple(input().split())
P_num = -1
Q_num = -1

onetoN = ""
for i in range(1, N+1):
    onetoN += str(i)

permu_list = list(itertools.permutations(onetoN))

for i in range(len(permu_list)):
    if P == permu_list[i]:
        P_num = i+1
    if Q == permu_list[i]:
        Q_num = i+1

print(abs(Q_num-P_num))

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

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

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

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

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

Solution (Python):
from itertools import permutations

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

l = list(permutations(range(1, n + 1)))
a = l.index(p)
b = l.index(q)

print(abs(a - b))

Solution (Python):
import itertools

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

if p == q:
    print(0)
    exit()
    
for i in itertools.permutations(l, n):
    count += 1
    if i == p:
        a = count
    elif i == q:
        b = count

print(max(a, b) - min(a, b))

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


import itertools
S = [str(i) for i in range(1,N+1)]
All = itertools.permutations(S) 
I = []
for a in All:
    a = list(a) 
    a = ''.join(a)
    I.append(a)
I.sort()
ind_p = I.index(''.join(P))
ind_q = I.index(''.join(Q))
print(abs(ind_p-ind_q))

Solution (Python):
from itertools import *

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

x=[i for i in range(1,N+1)]
X=list(permutations(x,N))
a,b=0,0

for index,i in enumerate(X):
    i=list(i)
    a= index+1 if i==P else a
    b= index+1 if i==Q else b
print(abs(a-b))

Solution (Python):
import itertools
 
n = int(input())
lis = [x for x in range(1,n+1)] 
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

A = map(str, P)
P = "".join(A)
B = map(str, Q)
Q = "".join(B)

permutations_lis = itertools.permutations(lis)

for i, one_case in enumerate(permutations_lis):
    C = map(str, one_case)
    R = "".join(C)
    if P == R:
        a = i + 1
    if Q == R:
        b = i + 1

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

Solution (Python):
import math
n=int(input())
def c():
    l=list(map(int, input().split()))
    s=0
    t=list(range(1,n+1))
    for i in range(n):
        s+=t.index(l[i])*math.factorial(n-i-1)
        t.remove(l[i])
    return s
print(abs(c()-c()))

Solution (Python):
import math
(n,),p,q=[list(map(int, i.split())) for i in open(0)]
def c(l):
    s=0
    t=list(range(1,n+1))
    for i in range(n):
        s+=t.index(l[i])*math.factorial(n-i-1)
        t.remove(l[i])
    return s
print(abs(c(p)-c(q)))

Solution (Python):
import itertools
(n,),p,q=[tuple(map(int, i.split())) for i in open(0)]
j=list(itertools.permutations(range(1, -~n)))
print(abs(j.index(p) - j.index(q)))

Solution (Python):

from itertools import permutations
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
q = list(map(lambda x: x-1, q))
p = list(map(lambda x: x-1, p))
a = [list(i) for i in permutations(range(n))]
print(abs(a.index(p)-a.index(q)))

Solution (Python):

import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):

    nums = [i for i in range(1, N + 1)]
    perm = list(itertools.permutations(nums, N))
    print(abs(perm.index(tuple(P)) - perm.index(tuple(Q))))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    P = [int(next(tokens)) for _ in range(N)]  
    Q = [int(next(tokens)) for _ in range(N)]  
    solve(N, P, Q)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):

    nums = [i for i in range(1, N + 1)]
    perm = list(itertools.permutations(nums, N))
    perm.sort()

    p_at = 0
    for i, pr in enumerate(perm):
        if pr == tuple(P):
            p_at = i + 1
        if pr == tuple(Q):
            q_at = i + 1
    print(abs(p_at - q_at))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    P = [int(next(tokens)) for _ in range(N)]  
    Q = [int(next(tokens)) for _ in range(N)]  
    solve(N, P, Q)


if __name__ == "__main__":
    main()

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

S = [0]*N
S[0] = 1
for s in range(1,N-1):
    S[s] = S[s-1]*(s+1)

def where(r):
    R = len(r)
    L = [0]*N
    now = 0
    for i in range(R):
        now += (r[i]-1-sum(L[0:r[i]]))*S[N-i-2]
        L[r[i]-1] = 1
    return now

a = where(p)
b = where(q)

print(abs(a-b))

Solution (Python):
import itertools

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

P = tuple(P)
Q = tuple(Q)

count = 0
pc = qc = -1
for per in itertools.permutations(range(1,N+1), N):
    if per == P:
        pc = count + 1
    if per == Q:
        qc = count + 1
    if pc != -1 and qc != -1:
        break
    count += 1

print(abs(pc - qc))

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

a = ans.index(p)
b = ans.index(q)
print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
ans = list(itertools.permutations(range(1,n+1)))

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

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

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

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

main()

Solution (Python):

import itertools


def main():
    N = int(input())
    PArry = list(map(int, input().split()))
    QArry = list(map(int, input().split()))

    a = 0
    b = 0
    comb = [i for i in range(1, N+1)]
    for i, iter in enumerate(itertools.permutations(comb)):
        listIter = list(iter)
        if PArry == listIter:
            a = i+1
        if QArry == listIter:
            b = i+1

    print(abs(a-b))


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import math

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
num = [x for x in range(1, n + 1)]
perm = itertools.permutations(num)
cnt = 0
a = 0
b = 0
for i in perm:
    i = list(i)
    cnt += 1
    if p == i:
        a = cnt
    if q == i:
        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 = list(itertools.permutations(P,n))

tmp = sorted(tmp)


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

Solution (Python):
import itertools
xx = int(input())

N = []
for i in range(xx):
    N.append(i+1)
    
X = [tuple(map(int,input().split())) for i in range(2)]

l = list(itertools.permutations(N,xx))
print(abs((l.index(X[1])+1) - (l.index(X[0])+1)) )

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

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


def main():
    n = int(input())
    ps = np.array(list(map(int, input().split())))
    qs = np.array(list(map(int, input().split())))

    nums = [i for i in range(1, n+1)]
    nums_array = np.array(list(permu(nums, n)))
    a = np.where(np.all(nums_array == ps, axis=1))[0][0]
    b = np.where(np.all(nums_array == qs, axis=1))[0][0]
    ans = abs(a - b)
    print(ans)


if __name__ == "__main__":
    main()

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

l = list(int(i) for i in range(1,n+1))
test = []
cnt = 0
for i in itertools.permutations(l,n):
	i = list(i)
	cnt += 1
	if p == i:
		cntp = cnt
	if q == i:
		cntq = cnt

print(abs(cntp - cntq))