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

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

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

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

Solution (Python):
import os, sys, re, math
import itertools

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

nums = [n for n in range(1, N + 1)]
patterns = itertools.permutations(nums, N)
i = 0
for pattern in patterns:
    if pattern == P:
        pi = i
    if pattern == Q:
        qi = i
    i += 1

print(abs(pi - qi))

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

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

a=None
b=None
for i, x in enumerate(X):
    if list(x) == p:
        a=i
        
    if list(x) == q:
        b=i
        
print(abs(a-b))

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

pos = [None, None]


def fn(digits, maxlen, index, pos):
    for x in range(1, maxlen + 1):
        if x in digits:
            continue
        ndigits = digits + [x]
        if len(ndigits) < maxlen:
            index = fn(digits + [x], maxlen, index, pos)
        else:
            if ndigits == p:
                pos[0] = index
            if ndigits == q:
                pos[1] = index
            index = index + 1
            
    return index


fn([], n, 0, pos)
print(abs(pos[0] - pos[1]))

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


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

a = 0
b = 0

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

Solution (Python):
N=int(input())
P= list(map(int, input().split()))
Q= list(map(int, input().split()))
if P==Q:
  print(0)
  exit()
dic=[]
Pn=0
Qn=0
import itertools
import numpy
l=numpy.arange(1,N+1,1,int)
for v in itertools.permutations(l, N):
  dic.append(v)
for i in range(len(dic)):
  if list(dic[i])==P:
    Pn=i
  elif list(dic[i])==Q:
    Qn=i
  elif Pn!=0 and Qn!=0:
    break
print(abs(Pn-Qn))

Solution (Python):
from itertools import permutations
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
for i,p in enumerate(permutations(range(1,N+1))):
    if p == P:
        pi = i
    if p == Q:
        qi = i

print(abs(pi-qi))

Solution (Python):
from itertools import permutations

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

order = list(permutations(list(range(1,n+1))))
turn_p = order.index(p)
turn_q = order.index(q)

print(abs(turn_p-turn_q))

Solution (Python):
from collections import deque

n = int(input())
p = list(map(int,input().split()))
z = list(map(int,input().split()))
p_n = 0
q_n = 0
for i in range(len(p)):
    if i == 0:
        p_n = p[i]
        q_n = z[i]
    else:
        p_n = p[i]+(p_n*10)
        q_n = z[i]+(q_n*10)


q = deque()
ans_list = []
n_list = [0 for i in range(n)]


for i in range(n):
    n_list[i] = 1
    tmp = []
    for j in n_list:
        tmp.append(j)
    q.append([i+1,tmp])
    n_list[i] = 0

while len(q) != 0:
    tmp = q.popleft()
    s = str(tmp[0])
    if len(s) == n:
        ans_list.append(tmp[0])
        continue
    for i in range(n):
        tmp_n = tmp[0]
        if tmp[1][i] == 0:
            tmp_l = []
            for j in range(n):
                tmp_l.append(tmp[1][j])
            tmp_l[i] = 1
            q.append([(i+1)+tmp_n*10,tmp_l])

ans_list = sorted(ans_list)

p_i = 0
q_i = 0

for i in range(len(ans_list)):
    if ans_list[i] == p_n:
        p_i = i
    if ans_list[i] == q_n:
        q_i = i

print(abs(p_i-q_i))

Solution (Python):
from itertools import *
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
dic = dict()
i = 0
for a in permutations(range(1,N+1),N):
    dic[a] = i
    i += 1
print(abs(dic[P]-dic[Q]))

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

all_num_list = list(it.permutations(range(1,N+1)))
all_num_list.sort()

P_num = all_num_list.index(P)
Q_num = all_num_list.index(Q)

print (abs(P_num-Q_num))

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 itertools import permutations
X = [i for i in range(1, int(input())+1)] 
X = list(permutations(X))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
print(abs(X.index(P)-X.index(Q)))

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

import itertools

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

cnt = 0
for i in li2:
    cnt +=1
    if i == p:
        a = cnt
    if i == q:
        b = cnt

print(abs(a-b))

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

import itertools

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

cnt = 0
for i in li2:
    cnt +=1
    if i == p:
        a = cnt
    if i == q:
        b = cnt

print(abs(a-b))

Solution (Python):
bit=[0]*9
fa=[1]*9
for n in range(1,9):fa[n]=fa[n-1]*n
def up(id):
    while id<9:
        bit[id]+=1
        id+=id&(-id)
def qr(id):
    res=0
    while id:
        res+=bit[id]
        id-=id&(-id)
    return res

x=int(input())
s=list(map(int,input().split()))
z=list(map(int,input().split()))

v=''

for n in s:
    a=qr(n)
    vl=n-1-a
    v+=str(vl)
    up(n)
v=v[::-1]
r1=0
for n in range(len(v)):
    r1+=int(v[n])*fa[n]

r2=0
v=''
bit=[0]*9
for n in z:
    a=qr(n)
    vl=n-1-a
    v+=str(vl)
    up(n)
v=v[::-1]
for n in range(len(v)):
    r2+=int(v[n])*fa[n]

print(abs(r1-r2))

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

permutations_N = list(permutations(range(1, N+1)))
a = permutations_N.index(P)
b = permutations_N.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()))

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

count = 0
for case in cases:
  count += 1
  if p == case:
    p_c = count
  if q == case:
    q_c = count
print(abs(p_c - q_c))

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

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 itertools
N=int(input())
c=list(itertools.permutations(range(1,N+1)))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
print(abs(c.index(p)-c.index(q)))

Solution (Python):
n=int(input())
*p,=map(int,input().split())
*q,=map(int,input().split())
if p==q:
    print(0)
else:
    import itertools
    l2=[]
    for i in itertools.permutations([i+1 for i in range(n)]):
        l2.append(list(i))
    print(abs(l2.index(p)-l2.index(q)))

Solution (Python):
import itertools

n = int(input())
p_li = tuple(map(int, input().split()))
q_li = tuple(map(int, input().split()))

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

s = list(itertools.permutations(n_li, len(n_li)))
s.sort()

p = 0
q = 0

for i in range(len(s)):
    if s[i] == p_li:
        p = i
    if s[i] == q_li:
        q = i

print(abs(p-q))

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

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

r = [i+1 for i in range(n)]
from itertools import permutations

count = 0
a = 0
b = 0
for rr in list(permutations(r)):
    count +=1
    if p == rr:
        a = count
    if q == rr:
        b = count

print(abs(a-b))

Solution (Python):
import itertools
import math
import sys

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

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

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

li = list(itertools.permutations(x))

for i in range(len(li)):
    if list(li[i]) == p:
        a = i
    elif list(li[i]) == q:
        b = i
print(abs(a-b))

Solution (Python):
from itertools import permutations
n = int(input())
p = ''.join([x for x in input().split()])
q = ''.join([x for x in input().split()])

a = [str(x) for x in range(1, n+1)]
a = [''.join(list(x)) for x in permutations(a)]

p_idx = -1
q_idx = -1
for i, x in enumerate(a):
  if p == x:
    p_idx = i
  if q == x:
    q_idx = i

print(abs(p_idx - q_idx))

Solution (Python):
import copy
N = int(input())
P = [int(a) for a in input().split()]
Q = [int(a) for a in input().split()]

nP = 0
nQ = 0
xs = []
def suretsu(x):
    if len(x) == N:
        xs.append(x)
        return
    else:
        for n in range(1, N+1):
            if n not in x:
                new_x = copy.deepcopy(x)
                new_x.append(n)
                suretsu(new_x)

suretsu([])
for i, x in enumerate(xs):
    is_P = True
    is_Q = True
    for j in range(N):
        if P[j]!=x[j]:
            is_P = False
        if Q[j]!=x[j]:
            is_Q = False
    if is_P:
        nP = i+1
    if is_Q:
        nQ = i+1

print(abs(nP-nQ))

Solution (Python):
from itertools import permutations

n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
S = sorted(P)
for i, C in enumerate(permutations(S)):
    if C == P:
        a = i
    if C == Q:
        b = i
print(abs(a - b))

Solution (Python):
import itertools

N = []
[N.append(num) for num in range(1, int(input())+1)]
Pn = tuple(map(int, input().split()))
Qn = tuple(map(int, input().split()))

permutation = []
for x in itertools.permutations(N):
    permutation.append(x)

print(abs(permutation.index(Pn) - permutation.index(Qn)))

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

pl = list(map(str, input().split())); p = int("".join(pl))
ql = list(map(str, input().split())); q = int("".join(ql))

import itertools
l = [str(i) for i in range(1, n+1)]
a, b = 0, 0
for v in list(itertools.permutations(l)):
    num = int("".join(list(v)))
    if p >= num:
        a += 1
    if q >= num:
        b += 1
print(abs(a-b))

Solution (Python):
import itertools

N=int(input())
P=int("".join(list(input().split())))
Q=int("".join(list(input().split())))
L=list(itertools.permutations(map(str, range(1,N+1)),N))
for i in range(len(L)):
  L[i]=int("".join(L[i]))
sorted(L)
print(abs(L.index(P)-L.index(Q)))

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

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

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import itertools

n = int(stdin.readline().rstrip())
p = [x for x in stdin.readline().rstrip().split()]
q = [x for x in stdin.readline().rstrip().split()]

x = ()
y = ()
for pp in p:
    x = x + (pp,)
for qq in q:
    y = y + (qq,)

c = []
for i in itertools.permutations([str(j) for j in range(1,n+1)]):
    c.append(i)
    
a = c.index(x)
b = c.index(y)

print (abs(b-a))

Solution (Python):
import sys
import time
import math
import itertools as it
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()


N = int(input())
P = inpl()
Q = inpl()
ls = P.copy()
ls.sort()
cnt = 0
a = 0
b = 0
for l in it.permutations(ls):
    cnt += 1
    l = list(l)
    if P == l:
        a = cnt
    if Q == l:
        b = cnt
print(abs(a - b))

ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

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

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

Solution (Python):
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
youso = []
zyunretu =[]
iti1 = 0
iti2 = 0
for x in range(n):
    youso.append(x+1)

for v in list(itertools.permutations(youso)):
    if str(a)[1 :-1] == str(v)[1 :-1] :
        iti1 = len(zyunretu)+1
    if str(b)[1 :-1] == str(v)[1 :-1] :
        iti2 = len(zyunretu)+1
    zyunretu.append(str(v)[1 :-1])

print(abs(iti1 - iti2))

Solution (Python):
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

aa, bb = 0, 0
for i in range(n):
    aa *= 10
    aa += a[i]
    bb *= 10
    bb += b[i]

p = [i for i in range(1, n+1)]
na, nb = 0, 0
cnt = 0
for v in itertools.permutations(p, n):
    cnt += 1
    vv = 0
    for i in range(n):
        vv *= 10
        vv += v[i]
    if aa == vv:
        na = cnt
    if bb == vv:
        nb = cnt

print(abs(na-nb))

Solution (Python):
from itertools import permutations
N = int(input())
a = list(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):
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 = list(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):
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())
Pls = list(map(int,input().split()))
Qls = list(map(int,input().split()))

lis = [i for i in range(1,N+1)]
permutations_lis = itertools.permutations(lis)
i = 0
for case in permutations_lis:
    i += 1
    if list(case) == Pls:
        a = i
    if list(case) == Qls:
        b = i

print(abs(a-b))

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
from itertools import permutations
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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


    for i, A in enumerate(permutations(range(1,N+1))):
        if A == P:
            p = i
        if A == Q:
            q = i

    print(abs(p-q))




if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations
lens = int(input())
a = tuple([int(x) for x in input().split()])
b = tuple([int(x) for x in input().split()])
perms = sorted(list(permutations(range(1, lens + 1))))

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

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
n = range(1,N + 1)
import itertools 
l = list(itertools.permutations(n))
a = 0
b = 0
for i in l:
    if i == tuple(P):
        a += 1
        break
    else:
        a += 1
for i in l:
    if  i == tuple(Q):
        b += 1
        break
    else:
        b += 1
print(abs(a - b))

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

Solution (Python):
import itertools as i
f=lambda:tuple(map(int,input().split()))
x=list(i.permutations(range(1,int(input())+1)))
print(abs(-~x.index(f())--~x.index(f())))

Solution (Python):
import itertools as i
input()
f=lambda:tuple(map(int,input().split()))
P=f()
Q=f()
x=sorted(list(i.permutations(P)))
print(abs(-~x.index(P)--~x.index(Q)))

Solution (Python):
import itertools as i
input()
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
x=sorted(list(i.permutations(P)))
print(abs(-~x.index(P)--~x.index(Q)))

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

Solution (Python):
from itertools import permutations as pe
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    ns=[]
    for i in range(1,n+1):
        ns.append(str(i))
    p=LISTS()
    q=LISTS()
    ct=0
    for v in pe(ns,n):
        v=list(v)
        
        ct+=1
        if p==v:
            a=ct
        if q==v:
            b=ct

    print(abs(a-b))
if __name__=='__main__':
    do()

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

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

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

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

A = list(itertools.permutations(list(range(1, n+1))))
A.sort()
p = None
q = None
for i in range(math.factorial(n)):
    if P == list(A[i]):
        p = i+1
    if Q == list(A[i]):
        q = i+1
    if p and q:
        print(abs(p - q))
        exit()

Solution (Python):



import itertools
import sys

n = input()
p = input()
q = input()

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

p = p.replace(' ', ', ')
p = "(" + p + ")"

q = q.replace(' ', ', ')
q = "(" + q + ")"

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

pcount = 0
qcount = 0
pf = False
qf = False

for i in range(len(b)):
    if str(b[i])==p:
        pcount = i+1
        pf = True
    if str(b[i])==q:
        qcount = i+1
        qf = True
    if pf and qf:
        break

print(abs(pcount-qcount))

Solution (Python):
import math
import itertools


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

min_num = []
cnt = 0
a = 0
b = 0

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

for v in itertools.permutations(min_num, n):
    cnt += 1
    if list(v) == P:
        a = cnt
    if list(v) == Q:
        b = cnt

print(abs(a-b))

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

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

resolve()

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

Solution (Python):
import itertools


N = int(input())





P = list(input().split())
Q = list(input().split())


P_str = ''.join(P)
Q_str = ''.join(Q)


all_cases = list(itertools.permutations(P))
all_cases_str = []
for l in all_cases:
    all_cases_str.append(''.join(l))
all_cases_str = sorted(all_cases_str)

a = 0
b = 0
cnt = 0

for s in all_cases_str:
    cnt += 1
    
    if P_str == s:
        a = cnt
    if Q_str == s:
        b = cnt

print(abs(a-b))

Solution (Python):
import itertools

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

for i,v in enumerate(itertools.permutations(range(1,N+1),N)):
  if tuple(P)==v:
    a=i
  if tuple(Q)==v:
    b=i
print(abs(a-b))

Solution (Python):
import math
N = int(input())
P= list(map(int, input().split()))
Q = list(map(int, input().split()))
p=0
q=0
L=[0]*(N+1)
L[0]=1
for i in range(N):
    L[P[i]]=1 
    
    
    num=0
    for y in range(1,P[i]+1):
        if L[y]==0:
            num+=1
    p+=num*math.factorial(N-i-1)
    
        
L=[0]*(N+1)
L[0]=1           
for i in range(N):
    L[Q[i]]=1 
    
    num=0
    for y in range(1,Q[i]+1):
        if L[y]==0:
            num+=1
    q+=num*math.factorial(N-i-1)
        

print(abs(q-p))

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

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

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

Solution (Python):
import sys
import numpy as np
from itertools import permutations, combinations
def input(): return sys.stdin.readline().rstrip()


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


if __name__ == '__main__':
    main()

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


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

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

  a = -1
  b = -1
  for i, seq in enumerate(orders):
    if seq == P:
      a = i
    if seq == Q:
      b = i

  print(abs(a-b))


if(__name__ == '__main__'):
  main()

Solution (Python):
import itertools

n = int(input())
a = list(range(1,n+1))
a = list(itertools.permutations(a,n))
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()))
tmp = list(itertools.permutations(range(1,n+1)))
tmp = sorted(tmp)
for index,t in enumerate(tmp):
  if list(t) == P and list(t) == Q:
    p = index+1
    q = index+1
  elif list(t) == P:
    p = index+1
  elif list(t) == Q :
    q = index+1
  
print(abs(p-q))

Solution (Python):
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
l=[i+1 for i in range(n)]
p_num=-1
q_num=-1
for i,peri in enumerate(itertools.permutations(l,n)):
    peri=list(peri)
    if p_num==-1:
        flag=True
        for j in range(n):
            if peri[j]!=p[j]:
                flag=False
                break
        if flag:
            p_num=i
    if q_num==-1:
        flag=True
        for j in range(n):
            if peri[j]!=q[j]:
                flag=False
                break
        if flag:
            q_num=i
    if p_num!=-1 and q_num!=-1:
        break
if p_num>q_num:
    print(p_num-q_num)
else:
    print(q_num-p_num)

Solution (Python):
import math

N = int(input())

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

def param(x):
  N = len(x)
  if N == 1:
    return 0
  
  p = x[0]
  y = x[1:N]
  for i in range(N-1):
    if y[i] > p:
      y[i] -= 1
  
  s = param(y)
  return s + int(math.factorial(N-1))*(p-1)

print(abs(param(P)-param(Q)))

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

Z = ['1','2','3','4','5','6','7','8']

X = Z[:N]

from itertools import permutations

A=[]
for l in list(permutations(X)):
  a = "".join(l)
  A.append(a)



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

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

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

m = 0
P_num = 0
Q_num = 0



for i in jr:
  if list(i) == P:
    P_num = m
  if list(i) == Q:
    Q_num = m
  m += 1
  

print(abs(P_num-Q_num))

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

test=[]
for i in range(1,N+1):
    test.append(str(i))
tables=list(itertools.permutations(test))

print(abs(tables.index(p)-tables.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)))
 
print(abs(l.index(P)-l.index(Q)))

Solution (Python):
import itertools

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


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

for x in range(len(a)):
  if a[x]==p:
    g=x
  if a[x]==q:
    f=x

print(abs(g-f))

Solution (Python):
from itertools import permutations
n = int(input())
n_2 = [i for i in range(1,n+1)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))

c = 1
ans = [0,0]
for i in permutations(n_2,n):
    
    if list(i) == p:
        ans[0] += c
    if list(i) == q:
        ans[1] += c
    c += 1

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

Solution (Python):
from collections import Counter, defaultdict
import sys

sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

def main():
    num = int(input())
    data1 = tuple(map(int, input().split()))
    data2 = tuple(map(int, input().split()))

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

    print(abs(p.index(data1) - p.index(data2)))




if __name__ == '__main__':
    main()

Solution (Python):

import itertools
import math
N = int(input())
P = tuple(map(int, input().split(" ")))
Q = tuple(map(int, input().split(" ")))


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

print(abs(a-b))

Solution (Python):
import math
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=[i for i in range(1,n+1)]
b=[i for i in range(1,n+1)]
p1=0
q1=0
for i in range(0,n):
  x=a.index(p[i])
  p1 +=math.factorial(n-1-i)*(x-1)
  a.remove(p[i])
  y=b.index(q[i])
  q1 +=math.factorial(n-1-i)*(y-1)
  b.remove(q[i])
print(abs(p1-q1))

Solution (Python):
import itertools
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
num_a=0
num_b=0
cnt=0
for v in itertools.permutations(list(range(1,n+1)), n):
    if v==a:
        num_a=cnt
    if v==b:
        num_b=cnt
    cnt+=1
print(abs(num_b-num_a))

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

Solution (Python):
import sys
from itertools import permutations


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    a, b = 0, 0
    for i, e in enumerate(permutations(range(1, n + 1))):
        if e == p:
            a = i
        if e == q:
            b = i
    print(abs(a - b))


if __name__ == "__main__":
    main()

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

Solution (Python):
from itertools import permutations

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

per_loop = map(list, (permutations(range(1, n+1))))
cnt = 0
x = []
for t in per_loop:
    if t == p or t == q:
        x.append(cnt)
    cnt += 1
if len(x) == 2: 
    print(abs(x[0]-x[1]))
else:
    print(0)

Solution (Python):
import itertools

def countOrder(n, plist, qlist):

    ans = 0
    p = 0
    q = 0

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

    for index, i in enumerate(irelist):
        if i == plist:
            p = index
        if i == qlist:
            q = index

    ans = abs(p-q)

    return ans

def main():
    n = int(input())
    plist = tuple(map(int, input().split()))
    qlist = tuple(map(int, input().split()))
    print(countOrder(n, plist, qlist))

if __name__ == '__main__':
    main()

Solution (Python):
import math
import itertools

def solve():
    n = math.factorial(N)

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


    j = 1
    for i in list(itertools.permutations(arr)):

        if i == tuple(P):
            a = j
            break
        j += 1
    j = 1
    for i in list(itertools.permutations(arr)):
        if i == tuple(Q):
            b = j
            break
        j += 1
    print(abs(a-b))    

if __name__ == "__main__":
    N = int(input())
    P = list(map(int, input().split()))  
    Q = list(map(int, input().split()))
    solve()