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())
N_list = [i for i in range(1,N+1)]

P = list(itertools.permutations(N_list))
P.sort()

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

Q_number = P.index(Q) + 1
O_number = P.index(O) + 1

print(abs(Q_number - O_number))

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

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

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ x for x in itertools.permutations(range(1, N + 1)) ]
a = [ key for key, val in enumerate(ls) if P == val ][0] + 1
b = [ key for key, val in enumerate(ls) if Q == val][0] + 1
print(abs(a - b))

Solution (Python):
import itertools

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


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

Solution (Python):
import itertools
import copy
n = int(input())
P = list(map(int, input().split()))
Q = tuple(list(map(int, input().split())))
P_ = copy.copy(P)
P_.sort()
p_list = list(itertools.permutations(P_))
P = tuple(P)

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

print(abs(a-b))

Solution (Python):
import itertools

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

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

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

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

for i in range(factorial(n)):
    if p==ls[i]:
        a = i
    if q==ls[i]:
        b = i

print(abs(a-b))

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

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

a = 0
b = 0

for i in range(len(l)):
    s = [i for i in l[i]]

    if s == p:
        a = i 
    if s == 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()))
r = list(itertools.permutations([i for i in range(1,n+1)]))
P , Q = (r.index(p)), (r.index(q))
print(abs(P-Q))

Solution (Python):
import sys
from itertools import permutations
N = int(input())
target_list = []
for i in range(2):
  target_list.append(tuple(map(int,input().split())))
  
list=[_ + 1 for _ in range(N)]
per=permutations(list,N)

ans = 0
counter = 1
if target_list[0] == target_list[1]:
  print("0")
  sys.exit()
for i in per:
  if i in target_list:
    
    if ans == 0:
      ans -= counter
    else:
      ans += counter
    
  counter += 1
print(ans)

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

l = [i+1 for i in range(N)]
for i, tpl in enumerate(itertools.permutations(l, N)):
    if tpl == P:
        a = i
    if tpl == Q:
        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()))

def library_number(x,n):
  l = list(range(1,n+1))
  tmp = 0
  for i in range(n):
    tmp += math.factorial(n-1-i)*l.index(x[i])
    l.remove(x[i])
  return tmp

ans = abs(library_number(p,n) - library_number(q,n))
print(ans)

Solution (Python):

import sys
import itertools


def solve(N: int, P: "List[int]", Q: "List[int]"):
    l = []
    for i in range(N):
        l.append(i+1)

    p = 0
    q = 0
    c = 0
    for i in itertools.permutations(l, N):
        c += 1
        t = list(i)
        if t == P:
            p = c
        if t == Q:
            q = c
        if p != 0 and q != 0:
            print(abs(q - p))
            break

    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 itertools
import math

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

per = [c+1 for c in range(n)]
per_list = list(itertools.permutations(per))

for i in range(math.factorial(n)):
    if p == per_list[i]:
        p_no = i
    if q == per_list[i]:
        q_no = i

print(abs(p_no - q_no))

Solution (Python):
import itertools

N = int(input())
l = list(itertools.permutations(range(1,N+1)))

A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

print(abs(l.index(A) - l.index(B)))

Solution (Python):



from itertools import permutations

N = int(input())
P = input().strip()
Q = input().strip()

nth = 0
np = -1 
nq = -1
for i in permutations(range(N)):
    nth += 1
    s = " ".join([str(x+1) for x in i])
    if s == P:
        np = nth
    if s == Q:
        nq = nth

print(abs(np - nq))

Solution (Python):
import itertools
n = int(input())
N = list(i for i in range(1,n+1))
L = itertools.permutations(N,n)
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
cnt, s, e = 0, 0, 0

for l in L:
  cnt += 1
  if a == l:
    s = cnt
  if b == l:
    e = cnt
print(abs(s-e))

Solution (Python):
import math

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


def find_order(num_list):
    if len(num_list) == 1:
        return 0
    first = num_list[0]
    order = sorted(num_list).index(first)
    if order == 0:
        return find_order(num_list[1:])
    else:
        return order * math.factorial(len(num_list) - 1) + find_order(num_list[1:])


order_p = find_order(P)
order_q = find_order(Q)

print(abs(order_p - order_q))

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

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

com=list(permutations(range(n)))

a=0
b=0
for i in range(len(com)):
    
    if com[i]==tuple(p):
        a=i
    if com[i]==tuple(q):
        b=i
print(abs(a-b))

Solution (Python):
from itertools import permutations

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

x = [i for i in range(1,n+1)]
ls = list(permutations(x))
i = 0
for per in ls:
    if per == p:
        a = i
    if per == q:
        b = i
    i += 1

print(abs(a-b))

Solution (Python):
import math
N=int(input())
A=list(map(lambda x:x-1,list(map(int,input().split()))))
B=list(map(lambda x:x-1,list(map(int,input().split()))))
a=0
for i in range(N):
    Apart=A[:i]
    order=0
    for aparts in Apart:
        if aparts<A[i]:
            order+=1
    a+=math.factorial(N-i-1)*(A[i]-order)
b=0
for i in range(N):
    Bpart=B[:i]
    order=0
    for bparts in Bpart:
        if bparts<B[i]:
            order+=1
    b+=math.factorial(N-i-1)*(B[i]-order)
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()))
P=sorted(list(permutations(range(1,n+1),n)))
print(abs(P.index(p)-P.index(q)))

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

import math

def dic_order(a):
    b =[x for x in range(1, n+1)] 
    cnt = 1
    for i in range(n - 1):
        cnt += b.index(a[i]) * math.factorial(n - i - 1)
        b.remove(a[i])
    return cnt

p_order = dic_order(p)
q_order = dic_order(q)
print(abs(p_order - q_order))

Solution (Python):
N = int(input())
from itertools import permutations

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

A=list(permutations([i for i in range(1, N+1)]))
a=A.index(P)
b=A.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()))

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

Solution (Python):
import math

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

for i in range(0,n,1):
    a += (p[i]-1)*math.factorial(n-i-1)
    b += (q[i]-1)*math.factorial(n-i-1)
    for  j in range(i+1,n,1):
        if p[j] > p[i]:
            p[j] -= 1
        if q[j] > q[i]:
            q[j] -= 1

print(abs(a-b))

Solution (Python):
from itertools import permutations

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
a = -1
b = -1
count = 0
for per in permutations(range(1,n+1)):
    count += 1
    arr = [i for i in per]
    if arr==p:
        a = count
    if arr==q:
        b = count

print(abs(a-b))

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

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

queue = [[]]
lst = []

while queue:
  e = queue.pop(-1)
  if len(e) == n:
    lst.append(tuple(e))
    continue
  for i in range(1,n+1):
    if i not in e:
      queue.append(e+[i])

lst.sort()
print(abs(lst.index(P) - lst.index(Q)))

Solution (Python):
import itertools

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

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

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

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

a = 0
b = 0
i = 0
for x in itertools.permutations(n):
    if list(x) == P:
        a = i
    if list(x) == Q:
        b = i
    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()))

    for i, per in enumerate(permutations(range(1, n + 1), n)):
        if per == P:
            a = i
        if per == Q:
            b = i

    print(abs(a - b))


if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

Solution (Python):


def check(A):
    tmp = 0
    first = A[0]
    A.pop(0)
    for i in A:
        if i < first:
            tmp += 1
    return tmp

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    con = [1, 1, 2, 6, 24, 120, 720, 5040, 40320]

    a, b = 0, 0

    for i in range(N, 0, -1):
        a += con[i - 1] * check(P)
        b += con[i - 1] * check(Q)

    print(abs(a - b))

if __name__ == "__main__":
    main()

Solution (Python):
import itertools

def main():
    N = int(input())
    P = input().replace(" ", "")
    Q = input().replace(" ", "")
    vals = []
    for comb in itertools.permutations(list(map(str, range(1, N+1))), N):
        vals.append("".join(comb))
    vals.sort()
    print(abs(vals.index(P) - vals.index(Q)))


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ x for x in itertools.permutations(range(1, N + 1), N) ]
a = [ key for key, val in enumerate(ls) if val == P ][0] + 1
b = [ key for key, val in enumerate(ls) if val == Q ][0] + 1
print(abs(a - b))

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

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


a = 0
b = 0

for x in pattern:
  new_x = list(x)
  if new_x == p:
    a = pattern.index(x)
  if new_x == q:
    b = pattern.index(x)


print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
s = ""
for i in range(n):
  s += str(i+1)
l = list(itertools.permutations(s, n))
p = tuple(input().split())
q = tuple(input().split())
a = l.index(p)
b = l.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()))

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

for a in range(9*8*7*6*5*4*3*2):
    if p == m[a]:
        x = a
        break
for b in range(9*8*7*6*5*4*3*2):
    if q == m[b]:
        y = b
        break
print(abs(x-y))

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

x = permutations(sorted(p))
t =0
s = 0;
c =0;
for i,j in enumerate(x):
	if p == list(j):
		s = i+1
	if q== list(j):
		c = i+1;
		
		
		
print(abs(s-c))

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

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 math
N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
S = [P, Q]
p = [0]*N
q = [0]*N

for i in range(N):
    for j in range(i):
        if P[i] > P[j]:
            p[i] += 1    
        if Q[i] > Q[j]:
            q[i] += 1

ans_p = 0
ans_q = 0

for i in range(N):
    ans_p += math.factorial(N-1-i) * (P[i]-1-p[i])
    ans_q += math.factorial(N-1-i) * (Q[i]-1-q[i])

print(abs(ans_p - ans_q))

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

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

Solution (Python):
import itertools,math
n = int(input())
a = sorted(list(itertools.permutations([ i for i in 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 = int(input().replace(' ', ''))
q = int(input().replace(' ', ''))
per = permutations([str(i) for i in range(1, n+1)], n)
lst = []
for pi in per:
  lst.append(int(''.join(pi)))
print(abs(lst.index(p) - lst.index(q)))

Solution (Python):

import sys
input=sys.stdin.readline
import itertools

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

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

if __name__=="__main__":
    main()

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

import itertools
permutations = list( itertools.permutations( range( 1, n + 1 ) ) )
p_order = permutations.index( p )
q_order = permutations.index( q )

print( abs( p_order - q_order ) )

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()))
num=0
numlist=[i for i in range(1,n+1)]
pall=list(itertools.permutations(numlist))
for pp in pall:
    if P==list(pp):
        pnum=num
    if Q==list(pp):
        qnum=num
    num+=1
print(abs(pnum-qnum))

Solution (Python):



from itertools import permutations
import sys

N = int(input())

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

perm = permutations([str(i) for i in range(1, N + 1)], N)

ls = []

for i in perm:
    ls.append(int("".join(i)))

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

Solution (Python):
import itertools

N = int(input())

l = []

for i in itertools.permutations(list(range(1,N+1))):
    l.append(i)

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

a = 1
b = 1

for i in l:
    if i < P:
        a += 1

for i in l:
    if i < Q:
        b += 1

print(abs(a-b))

Solution (Python):
from itertools import permutations
n=int(input())
p,q = tuple(map(int, input().split())), tuple(map(int, input().split()))
l = list(range(1,n+1))
x = [i for i in permutations(l,n)]
print(abs(x.index(p) - x.index(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)
    exit()

L = list(range(1, N+1, 1))

p = itertools.permutations(L, N)

cn = 0
cn1 = 0
cn2 = 0

for i in p:
    i = list(i)

    cn = cn + 1
    if i == P:
       cn1 = cn
    elif i == Q:
       cn2 = cn
       

print(abs(cn1 - cn2))

Solution (Python):
from itertools import permutations

n = int(input())
x, y = tuple(map(int, input().split())), tuple(map(int, input().split()))
p = [per for per in permutations(list(range(1, n+1)), n)]
print(abs(p.index(x) - p.index(y)))

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
def zyunretu(s,y):
    l=[]
    for i in range(y,n):
        l.append(s[i])
    l.sort()
    for i in range(len(l)):
        if l[i]==s[y]:
            return i+1
    
def kaizyou(a):
    if a==1:
        return 1
    else:
        return a*kaizyou(a-1)
    
def num(x):
    a=0
    for i in range(n-1):
        a+=(zyunretu(x,i)-1)*kaizyou(n-i-1)
    return a
print(abs(num(p)-num(q)))

Solution (Python):
import itertools

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

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

tempP=0
tempQ=0

for i in range(len(L)):

	if list(L[i])==P:
		tempP=i
	if list(L[i])==Q:
		tempQ=i

print(abs(tempP-tempQ))

Solution (Python):
import itertools
import math

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

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

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

Solution (Python):
import itertools
N = int(input())
P=tuple(int(c) for c in input().split())
Q=tuple(int(c) for c in input().split())

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    P=LI()
    Q=LI()
    
    
    import itertools
    
    cnt=0
    p=0
    q=0
    for ite in itertools.permutations(range(1,N+1)):
        if P==list(ite):
            p=cnt
        if Q==list(ite):
            q=cnt    
        cnt+=1
        
    print(abs(p-q))
        
        
main()

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

import itertools

n_l = [ i+1 for i in range(n) ]
d = {}
for i, v in enumerate(itertools.permutations(n_l)):
    d[str(v)] = i
print(abs(d[p]-d[q]))

Solution (Python):
import itertools
 
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
num = [i+1 for i in range(N)]
 
p_ord = 0
q_ord = 0
cnt = 0
for pre in itertools.permutations(num, N):
    if P == list(pre):
        p_ord = cnt
 
    if Q == list(pre):
        q_ord = cnt
    cnt += 1
 
print(abs(p_ord-q_ord))

Solution (Python):

import itertools

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

p_ord = 0
q_ord = 0
cnt = 0
for pre in itertools.permutations(num, N):
    if P == list(pre):
        p_ord = cnt

    if Q == list(pre):
        q_ord = cnt
    cnt += 1

print(abs(p_ord-q_ord))

Solution (Python):
def nanbanme(n, x):
    lst = [1 for i in range(n)]
    ans = 0
    for i in range(n):
        a = x[i]
        count = 0
        for j in range(a - 1):
            if (lst[j] == 1):
                count = count + 1
        tmp = 1
        for k in range(1, n - i):
            tmp = tmp*k
        ans = ans + count*tmp
        lst[a - 1] = 0
    return (ans + 1)

n = int(input())
lst1 = list(map(int,input().split()))
lst2 = list(map(int,input().split()))

a = nanbanme(n, lst1)
b = nanbanme(n, lst2)

print(abs(a - b))

Solution (Python):
from itertools import permutations

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

nums =[i for i in range(1,N+1)]
pers =permutations(nums,N)
count = 0
for i in pers:
  count += 1
  if P == list(i):
    a = count
  if Q == list(i):
    b = count

print(abs(a-b))

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)]))
 
a, b = 0, 0
for i, R in enumerate(permutations):
  if R == P: a = i
  if R == Q: b = i

print(abs(a-b))

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



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

print(abs(a-b))

Solution (Python):
import math

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

def check(k,a):
  l = len(a)
  if l == 2:
    if a[0] > a[1]:
      return 2
    else:
      return 1
  for i in range(l):
    if a[i] > k:
      a[i] -= 1
  return (a[0]-1) * math.factorial(l-1) + check(a[0],a[1:l])

print(abs(check(10,p) - check(10,q)))

Solution (Python):
N = int(input())
L = [0]
M = list(range(1 , N + 1))
a = 1
for i in range(1 , N):
  for j in range(1 ,i + 1):
    a = a * j
  L.append(a)
  a = 1
list.sort(L,reverse = True)
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
con_P = 0
con_Q = 0
for k in range(0 , N - 1):
  if k != N - 2:
    con_P += L[k] * (M.index(P[k]))
    M.remove(P[k])
  else:
    con_P += L[k] * (M.index(P[k]) + 1)
    
M = list(range(1 , N + 1))

for l in range(0 , N - 1):
  if l != N - 2:
    con_Q += L[l] * (M.index(Q[l]))
    M.remove(Q[l])
  else:
    con_Q += L[l] * (M.index(Q[l]) + 1)
ans = con_P - con_Q
if ans < 0:
  print(-ans)
else:
  print(ans)

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

Solution (Python):
import itertools

n = int(input())
p = map(int, input().split())
q = map(int, input().split())
d = dict()
for i, v in enumerate(itertools.permutations([x for x in range(1, n+1, 1)]), 1):
  d[str(v)] = i

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

Solution (Python):

import itertools

n = int(input())
a = tuple(list(map(int,input().split())))
b = tuple(list(map(int,input().split())))
list = [num for num in range(1, n + 1)]

count = 1
aidx = bidx = 0
for i in itertools.permutations(list, n):
    if i == a: aidx = count
    if i == b: bidx = count
    if aidx != 0 and bidx != 0: break
    count += 1

print(abs(aidx - bidx))

Solution (Python):
import math
import itertools


def calc_digit_sum(num):
    digits = sums = 0
    while num > 0:
        digits += 1
        sums += num % 10
        num //= 10
    return digits, sums


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

pidx, qidx = 0, 0
candidates = list(itertools.permutations(range(1, n+1), n))
for index, candidate in enumerate(candidates):
    candidate = list(candidate)

    if candidate == p:
        pidx = index
    if candidate == q:
        qidx = index

print(abs(pidx - qidx))

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

P=[]
Q=[]
for i in itertools.permutations(p):
    P.append(i)
for i in itertools.permutations(q):
    Q.append(i)
P=sorted(P)
Q=sorted(Q)
p=tuple(p)
q=tuple(q)
for i in range(math.factorial(N)):
    if P[i]==p:
        a=i+1
        break
for i in range(math.factorial(N)):
    if Q[i]==q:
        b=i+1
        break

print(abs(a-b))

Solution (Python):

"""
Created on Tue Sep  8 16:35:44 2020

@author: liang
"""


N = int(input())
P_list = list()

num = list(range(N))
perm = list()

def make_perm():
    
    if len(num) == 0:
        P_list.append(perm.copy())
        
        return
    for i in range(len(num)):
        t = num.pop(0)
        perm.append(t)
        
        make_perm()
        perm.pop()
        num.append(t)
        
make_perm()

P_list.sort()


ans = 0
P = [int(x)-1 for x in input().split()]
Q = [int(x)-1 for x in input().split()]
for i in range(len(P_list)):
    if P == P_list[i]:
        a = i
    if Q == P_list[i]:
        b = i
print(abs(a-b))

Solution (Python):
from itertools import permutations as pu
n = int(input())
p = tuple([int(_) for _ in input().split()])
q = tuple([int(_) for _ in input().split()])
ls = list(pu(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()))

a = b = None

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

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

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

N_dict = {v:(i+1) for i,v in enumerate(itertools.permutations(N, n))}


print(abs(N_dict[P] - N_dict[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 a in range(len(A)):
    if A[a] == P:
        p = a
    if A[a] == Q:
        q = a

print(abs(p - q))

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

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

Solution (Python):
from itertools import permutations


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

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

Solution (Python):
import itertools as it

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

r = list(it.permutations(range(1, n+1)))

a = 0
b = 0
for i in range(len(r)):
    if r[i] == p:
        a = i
    if r[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()))

if P == Q:
  print(0)
  exit(0)
  
import itertools as it

S = list(it.permutations(range(1,N+1)))
pi,qi = -10,-10
for i in range(len(S)):
  if S[i] == P:
    pi = i
  elif S[i] == Q:
    qi = i
  if pi >= 0 and qi >= 0:
    break

ans = abs(pi-qi)
print(ans)

Solution (Python):
import itertools
def main():
    n = int(input())
    p_list = list(itertools.permutations ([i+1 for i in range(n)]))
    p = tuple(map(int, input().split(" ")))
    q = tuple(map(int, input().split(" ")))
    pn = 0
    qn = 0
    for i in range(len(p_list)):
        if p == p_list[i]:
            pn = i
        if q == p_list[i]:
            qn = i
    print(abs(pn-qn))

if __name__ == "__main__":
    main()

Solution (Python):
from itertools import permutations

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

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

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

def main():
 
    input = sys.stdin.readline
 
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
 
    L = list(permutations(list(range(1, n + 1))))
    print(abs(L.index(p) - L.index(q)))
 
 
if __name__ == "__main__":
    main()