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()))
Nlist = []
for i in range(N):
    Nlist.append(i+1)

J = list(itertools.permutations(Nlist, N))

count = 0
for i in range(len(J)):
    if J[i] == P:
        a = i+1
        count += 1
        if count == 2:
            break
    if J[i] == Q:
        b = i+1
        count += 1
        if count == 2:
            break

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

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

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

ans = 0
for i, check_i in enumerate(check_list, 1):
  
  if list(check_i) == P:
    ans += i
  
  if list(check_i) == Q:
    ans -= i
    
print(abs(ans))

Solution (Python):

from itertools import permutations

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

rank_P = rank_Q = 0

for i, t in enumerate(permutations(range(1, N + 1)), start=1):
    if t == P:
        rank_P = i
        if rank_Q:
            break

    if t == Q:
        rank_Q = i
        if rank_P:
            break

print(abs(rank_P - rank_Q))

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

arr = list(itertools.permutations(range(1,n+1),n))
arr = sorted(arr)
for i in range(len(arr)):
    if list(arr[i]) == p:
        a = i
    if list(arr[i]) == q:
        b = i
print(abs(b-a))

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

iter = list(itertools.permutations(range(1,N+1)))
a = iter.index(A)
b = iter.index(B)

print(abs(a-b))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from itertools import permutations
inf = 10**17
mod = 10**9+7

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

cnt = 1
a = 0
b = 0
a = [i for i in range(1, n + 1)]
for v in permutations(a):
    v = list(v)
    if p == v: a = cnt
    if q == v: b = cnt
    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()))
a = 0
b = 0

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

print(abs(a-b))

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

import itertools

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




tmp1=ptr.index(P) 
tmp2=ptr.index(Q) 

print(abs(tmp1-tmp2))

Solution (Python):
from itertools import permutations

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

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


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

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

retu=range(1,n+1)
cnt=0
cp=0
cq=0
for r in itertools.permutations(retu):
    rr = [r[i] for i in range(n)]
    cnt+=1
    if rr == p:
        cp=cnt
    if rr == q:
        cq=cnt
print(abs(cp-cq))

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

p = tuple(p)
q = tuple(q)

numbers = [i for i in range(n)]
l = list(itertools.permutations(numbers))

p_order = l.index(p)
q_order = l.index(q)

ans = abs(p_order - q_order)
print(ans)

Solution (Python):
from itertools import permutations

n = int(input())
in_list = [int(w) for w in input().split()]
p_li = tuple(in_list)
q_li = tuple([int(w) for w in input().split()])


permu = permutations(sorted(in_list))

p_in = 0
q_in = 0

for i, p in enumerate(permu):
    if p == p_li:
        p_in = i
    if p == q_li:
        q_in = i

print(abs(p_in-q_in))

Solution (Python):
import sys
from itertools import permutations

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *PQ = map(int, read().split())
    P = tuple(PQ[:N])
    Q = tuple(PQ[N:])

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


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
p_permu=sorted(list(permutations(p)))
q_permu=sorted(list(permutations(q)))
print(abs(p_permu.index(tuple(p))-q_permu.index(tuple(q))))

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

Solution (Python):
from itertools import permutations
N = input()
P = tuple([int(_) for _ in input().split()])
Q = tuple([int(_) for _ in input().split()])
pl = list(permutations(P))
pl.sort()
ql = list(permutations(Q))
ql.sort()

pi = pl.index(P)+1
qi = ql.index(Q)+1
print(abs(pi-qi))

Solution (Python):
import itertools

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

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

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

Solution (Python):
def main():
    import itertools

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

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

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

X = sorted(itertools.permutations(p, N))

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

x=sorted(itertools.permutations(p,n))

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

print(abs(a-b))

Solution (Python):
from itertools import permutations

N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
temp=list(permutations(range(1,N+1)))
print(abs(temp.index(P)-temp.index(Q)))

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

dict_list = sorted(list(itertools.permutations(a, len(a))))

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

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), n))
idx1 = permutation.index(p)
idx2 = permutation.index(q)
print(abs(idx1 - idx2))

Solution (Python):
from itertools import permutations

n = int(input())

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

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

Solution (Python):
from itertools import permutations
n=int(input())
p=tuple(input().split())
q=tuple(input().split())
a=sorted(list(permutations(p)))
print(abs(a.index(p)-a.index(q)))

Solution (Python):









import sys
import itertools
import math
input = sys.stdin.readline

N = int(input())
list_P = tuple(map(int, input().split()))
list_Q = tuple(map(int, input().split()))

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

Pi = List_All.index(list_P)
Qi = List_All.index(list_Q)
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()))

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

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

Solution (Python):
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
test=[]
for i in itertools.permutations(range(1,N+1)):
    test.append(''.join(list(map(str,i))))
print(abs(test.index(''.join(list(map(str,P))))-test.index(''.join(list(map(str,Q))))))

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

o = {}
for i, p in enumerate(permutations(range(1,N+1))):
  	o[p] = i
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
print(abs(o[P] - o[Q]))

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

def to_str(string, array):
    for i in array:
        string += str(i)
    return string

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

    p_str = ""
    q_str = ""
    p_str = to_str(p_str, p)
    q_str = to_str(q_str, q)
    
    arr = [i+1 for i in range(n)]

    nums = list(itertools.permutations(arr))
    nums_str = []
    for e in nums:
        temp = ""
        temp = to_str(temp, e)
        nums_str.append(temp)

    nums_str.sort()

    a = nums_str.index(p_str) + 1
    b = nums_str.index(q_str) + 1
    print(abs(a-b))

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
N=int(input())
L=list(map(int,input().split()))
R=list(map(int,input().split()))
P = list(itertools.permutations(L, N))
P=sorted(P)
a=P.index(tuple(L))
b=P.index(tuple(R))
print(abs(a-b))

Solution (Python):



import math




def numbering(P,N):

    used = [1]*(N+1)
    all = math.factorial(N)
    ans =0
    for i in range(N):
        p = P.pop(0)
        used[p] =0
        all =int(all/(N-i))
        ans += all *(sum(used[0:p])-1)
 

    return ans+1




def main():
    N = int(input())
    P = list(map(int,input().split()))
    ans = numbering(P,N)
    Q = list(map(int,input().split()))
    ans2 = numbering(Q,N)
    print(abs(ans - ans2))
if __name__ == "__main__":
    main()

Solution (Python):
from itertools import permutations

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

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

a = 0
b = 0

for i, name in enumerate(list(permutations(ls, N))):
    name = list(name)
    if name == P:
        a = i
    if name == 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()))

t = 0
x = 0
y = 0
for i in permutations(range(1,n+1),n):
  t += 1
  if i == p: x = t
  if i == q: y = t

print(abs(x-y))

Solution (Python):
import itertools

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
L = list(itertools.permutations(list(range(1,n+1))))
p_a = 1
p_b = 1

for i,l in enumerate(L):
    if p == list(l):
        p_a = i+1
    if q == list(l):
        p_b = i+1

print(abs(p_a-p_b))

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

import itertools

P_num = 0
Q_num = 0

for num, i in enumerate(itertools.permutations(N)):
    if P == i:
        P_num = num
        
    if Q == i:
        Q_num = num
        
print(abs(P_num-Q_num))

Solution (Python):
from itertools import permutations
n=int(input())
p=tuple(int(i) for i in input().split())
q=tuple(int(i) for i in input().split())

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

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

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

a = list(permutations(l))

print(abs(a.index(p1) - a.index(p2)))

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)+1
b = l.index(q)+1

print(abs(a-b))

Solution (Python):
from math import factorial

n=int(input())
p=[int(i)-1 for i in input().split()]
q=[int(i)-1 for i in input().split()]

plst=[i for i in range(n)]
qlst=[i for i in range(n)]
pls=[]
qls=[]

for i in p:
    a=plst.index(i)
    pls.append(a)
    plst.remove(i)
for i in q:
    a=qlst.index(i)
    qls.append(a)
    qlst.remove(i)

ansp=1
ansq=1

for i in range(n):
    ansp+=pls[i]*(factorial(n-i-1))
for i in range(n):
    ansq+=qls[i]*(factorial(n-i-1))
    
print(abs(ansp-ansq))

Solution (Python):
from math import factorial

n=int(input())
p=[int(i)-1 for i in input().split()]
q=[int(i)-1 for i in input().split()]

plst=[i for i in range(n)]
qlst=[i for i in range(n)]
pls=[]
qls=[]

for i in p:
    a=plst.index(i)
    pls.append(a)
    plst.remove(i)
for i in q:
    a=qlst.index(i)
    qls.append(a)
    qlst.remove(i)

ansp=1
ansq=1

for i in range(n):
    ansp+=pls[i]*(factorial(n-i-1))
for i in range(n):
    ansq+=qls[i]*(factorial(n-i-1))
    
print(abs(ansp-ansq))

Solution (Python):

import itertools

n = int(input())

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

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

s = list(itertools.permutations(p, n))

s.sort()

print(abs(s.index(tuple(p))-s.index(tuple(q))))

Solution (Python):
n= int(input())
import sys
pn = list(map(int, sys.stdin.readline().split()))
qn = list(map(int, sys.stdin.readline().split()))

a = 0
b= 0
m = [1]*(n+1)
m[0]=0

import math

for i in pn:

    if sum(m)==2:
        a += sum(m[:i]) +1
        break
    m[i] = 0

    a += sum(m[:i])* math.factorial(sum(m))


m = [1]*(n+1)
m[0]=0

for i in qn:

    if sum(m)==2:
        b += sum(m[:i]) +1
        break
    m[i] = 0

    b += sum(m[:i])* math.factorial(sum(m))


print(abs(a-b))

Solution (Python):
from itertools import permutations

n=int(input())
l=list(map(str,input().split()))
t=list(map(str,input().split()))
s="".join(l)
h="".join(t)

l1=list()

for i in permutations(s):
    k="".join(i)
    l1.append(k)
 
l1.sort()

print(abs(l1.index(s)-l1.index(h)))

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
for i, perm in enumerate(permutations(list(range(1, N+1))), 1):
    if perm == P:
        a = i
    if perm == Q:
        b = i
    if a != 0 and b != 0:
        break
print(abs(a - b))

Solution (Python):
n = int(input())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
p_d = 0
q_d = 0
cnt = 0
if ps == qs:
    print(0)
    exit()
from itertools import permutations as perm
perms = list(range(1,n+1))
for p in perm(perms):
    if list(p) == ps:
        p_d = cnt
    elif list(p) == qs:
        q_d = cnt
    cnt += 1
print(abs(p_d-q_d))

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

l = []
for i in range(1, N+1):
  l.append(i)
  
p_list = list(itertools.permutations(l))
a = p_list.index(P)
b = p_list.index(Q)
ans = abs(b-a)
print(ans)

Solution (Python):
import itertools

N=int(input())
p=list(map(str,input().split()))
q=list(map(str,input().split()))
P=''.join(p)
Q=''.join(q)

ns=list(itertools.permutations(range(1,N+1)))
Ns=[]
for i in range(len(ns)):
    temparr=[]
    for j in range(N):
        a=ns[i][j]
        temparr.append(str(a))
    b=''.join(temparr)
    Ns.append(b)

PO=Ns.index(P)
QO=Ns.index(Q)
print(abs(PO-QO))

Solution (Python):
import itertools

N = int(input())
ORIGIN = tuple(range(1, N+1))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a , b = 0, 0

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

print(abs(a-b))

Solution (Python):
from itertools import permutations

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

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

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

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

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

import math

def check(l, N):
    ls = [i for i in range(N)]
    number = 0
    for i in range(N-1):
        number += math.factorial(N-1-i) * ls.index(l[i]-1)
        ls.remove(l[i]-1)
    number += 1
    return number

ans = check(P, N) - check(Q, N)
print(abs(ans))

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

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
p=lmp()
q=lmp()
l=list(itertools.permutations([i for i in range(1,n+1)]))
print(abs(l.index(tuple(p))-l.index(tuple(q))))

Solution (Python):
from bisect import bisect_left
from itertools import permutations
n=int(input())
p=''.join(map(str,input().split()))
q=''.join(map(str,input().split()))
l=list(range(1,n+1))
A=list(permutations(l,n))
A=[''.join(map(str,i)) for i in A]
A.sort()
a=bisect_left(A,p)
b=bisect_left(A,q)
print(abs(a-b))

Solution (Python):
import itertools

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

kaijolist = [1]
kaijo = 1

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

for i, p in enumerate(j):
    if p == P:
        a = i + 1
                                  
for i, q in enumerate(j):
    if q == Q:
        b = i + 1

print(abs(a - b))

Solution (Python):
import itertools

if __name__ == "__main__":
    N = int(input())
    P = [int(x) for x in input().split(" ")]
    Q = [int(x) for x in input().split(" ")]

    a = 1
    b = 1
    for x in itertools.permutations(range(1, N + 1)):
        if list(x) == P:
            break
        a += 1

    for x in itertools.permutations(range(1, N + 1)):
        if list(x) == Q:
            break
        b += 1
    print(abs(a - b))

Solution (Python):
import itertools

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

N = list(itertools.permutations(n))
print(abs(N.index(p)-N.index(q)))

Solution (Python):
import itertools

n = sorted([x+1 for x in range(int(input()))])
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
count = 0
pv = 0
qv = 0
for i in itertools.permutations(n,len(n)):
  count += 1
  if i == p:
    pv = count
  if i == q:
    qv = count
print(abs(pv-qv))

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

pn = 0
qn = 0

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


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

Solution (Python):




import itertools 

n = int(input())
p = 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 resolve():
    N = int(input())
    P = input().split()
    Q = input().split()
    Fac = [1] * (N+1)
    for i in range(1,N+1):
        Fac[i] = Fac[i-1] * i
    A = 0
    Alist = list(range(0,N))
    B = 0
    Blist = list(range(0,N))
    for i in range(N): 
        A += Alist[int(P[i]) - 1] * Fac[N-1-i]
        for j in range(int(P[i])-1, N):
            Alist[j] -= 1
        B += Blist[int(Q[i]) - 1] * Fac[N-1-i]
        for j in range(int(Q[i])-1, N):
            Blist[j] -= 1
    print(abs(A-B))
resolve()

Solution (Python):
from itertools import permutations

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

a = 0
b = 0

for i, p in enumerate(permutations(range(1, N+1), N)):
    if p == P:
        a = i
    if p == 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()))

counter = 0
a = 0
b = 0
for i in permutations(range(1,N+1)):
    if i == P:
        a = counter
    if i == Q:
        b = counter
    counter += 1
print(abs(a-b))

Solution (Python):
import itertools
import math
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
X = sorted(itertools.permutations(range(1,N+1)))


qn = 0
num = 0
for i in X:
    num += 1
    if i == P:
        pn = num
    if i == Q:
        qn = num
print(abs(pn-qn))

Solution (Python):
import itertools

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

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

Solution (Python):
import itertools

N = int(input())
P_n = tuple(map(int, input().split(' ')))
Q_n = tuple(map(int, input().split(' ')))

a = list(itertools.permutations(range(1, N+1))).index(P_n)
b = list(itertools.permutations(range(1, N+1))).index(Q_n)
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):

import itertools
import sys
import math
'''
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

a, b = map(int, readline().split())
print(a)

lis = [int(readline().rstrip('\n')) for _ in range(b)]
print(lis)
c = int(read())
print(c)
'''


read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
'''
a, b = map(int, readline().split())
print(a)

lis = [int(readline()) for _ in range(b)]
print(lis)
c = int(read())
print(c)
'''


n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
ansList = [i for i in range(1, n + 1)]

div = n // 2

com = list(itertools.permutations(ansList))


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

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

Solution (Python):
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
from itertools import permutations as per
tuples = per(range(1, N+1))
 
a = 0
b = 0
for i, t in enumerate(tuples):
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a - b))

Solution (Python):
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
from itertools import permutations as per
tuples = per(range(1, N+1))
 
a = 0
b = 0
for i, t in enumerate(tuples):
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a - b))

Solution (Python):
f=lambda:tuple(map(int,input().split()))
n=f()[0]
from itertools import *
p=[*permutations(range(1,n+1))].index
print(abs(p(f())-p(f())))

Solution (Python):
n=int(input())
from itertools import *
p=[*permutations(range(1,n+1))]
f=lambda:p.index(tuple(map(int,input().split())))
print(abs(f()-f()))

Solution (Python):
f=lambda:tuple(map(int,input().split()))
n=f()[0]
from itertools import *
p=[*permutations(range(1,n+1))]
print(abs(p.index(f())-p.index(f())))

Solution (Python):
from itertools import permutations

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

noP, noQ = 0, 0
for no, Rs in enumerate(permutations(range(1, N+1), r=N), start=1):
    if Ps == Rs:
        noP = no
    if Qs == Rs:
        noQ = no

print(abs(noP-noQ))

Solution (Python):
import itertools
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
p=P.copy()
p.sort()
p=itertools.permutations(p)
j=0
for i in p:
    j+=1
    if list(i)==P:
        pcount=j
    if list(i)==Q:
        qcount=j
print(abs(pcount-qcount))

Solution (Python):
import itertools

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

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

pi = 0
for i, a in enumerate(p):
    if a == tuple(P):
        pi = i
        break
qi = 0
for i, a in enumerate(q):
    if a == tuple(Q):
        qi = i
        break
print(abs(pi-qi))

Solution (Python):
import itertools
n = int(input())
l = [i for i in range(1,n+1)]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
d = list(itertools.permutations(l, n))
p_ind = 0
q_ind = 0
for i in range(len(d)):
  if p == d[i]:
    p_ind = i
  if q == d[i]:
    q_ind = i
print(abs(p_ind-q_ind))

Solution (Python):
import itertools
n = int(input())
l = [i for i in range(1,n+1)]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
d = list(itertools.permutations(l, n))
p_ind = 0
q_ind = 0
for i in range(len(d)):
  if p == d[i]:
    p_ind = i
  if q == d[i]:
    q_ind = i
print(abs(p_ind-q_ind))

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

ansp=1
ansq=1
f=1
for i in range(1,n):
    t=0
    f*=i
    for j in range(1,i+1):
        if p[-i-1]>p[-j]:
            t+=1
    ansp+=t*f
f=1
for i in range(1,n):
    t=0
    f*=i
    for j in range(1,i+1):
        if q[-i-1]>q[-j]:
            t+=1
    ansq+=t*f

print(abs(ansp-ansq))

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

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

Solution (Python):
import sys
import itertools

import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

perms = []
x = [i for i in range(1, N + 1)]
def perm(start):
    if start == N:
        perms.append(x.copy())
        return
    for i in range(start, N):
        x[i], x[start] = x[start], x[i]
        perm(start + 1)
        x[i], x[start] = x[start], x[i]
perm(0)
perms = sorted(perms)

a, b = 0, 0
for i in range(len(perms)):
    R = perms[i]
    for j in range(len(P)):
        if P[j] != R[j]:
            break
    else:
        a = i

    for j in range(len(Q)):
        if Q[j] != R[j]:
            break
    else:
        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()]
OrderP = 1
OrderQ = 1
for T in range(0,N-1):
    OrderP += sorted(P[T:]).index(P[T])*math.factorial(N-1-T)
    OrderQ += sorted(Q[T:]).index(Q[T])*math.factorial(N-1-T)
print(abs(OrderP-OrderQ))

Solution (Python):
import itertools

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

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

x1 = pls.index(a)
x2 = pls.index(b)
print(abs(x1 - x2))

Solution (Python):
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
pp = int("".join(map(str, p)))
qq = int("".join(map(str, q)))
ppp = 0
qqq = 0

for i in itertools.permutations(p,n):
  j = int("".join(map(str, i)))
  if pp > j:
    ppp += 1
for i in itertools.permutations(q,n):
  j = int("".join(map(str, i)))
  if qq > j:
    qqq += 1
    
print(abs(ppp-qqq))

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

Solution (Python):
import itertools

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

if P == Q:
  print(0)
  exit()

cnt = a = b = 0

for v in list(itertools.permutations(L)):
  cnt += 1
  if list(v) == P:
    a = cnt
  elif list(v) == Q:
    b = cnt

print(abs(a-b))

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

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

a = prm.index(p)
b = prm.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()))
 
for v_idx,v in enumerate(itertools.permutations(range(1,N+1))):
    if v==P:
        a=v_idx
    if v==Q:
        b=v_idx
        
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()))

    a = None
    b = None

    perms = tuple(permutations(range(1, n + 1), n))

    for i in range(len(perms)):
        if a is None and tuple(perms[i]) == p:
            a = i
        if b is None and tuple(perms[i]) == q:
            b = i
        if a is not None and b is not None:
            print(abs(a - b))
            exit()


if __name__ == '__main__':
    main()

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

for i in range(N-1):
  p+=sum(list(1 if P[i]>P[k] else 0 for k in range(i,N)))*math.factorial(N-i-1)
  q+=sum(list(1 if Q[i]>Q[k] else 0 for k in range(i,N)))*math.factorial(N-i-1)
print(abs(p-q))

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

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

for i in range(len(al)):
    if al[i]==p:
        p_ord=i
    if al[i]==q:
        q_ord=i

print(abs(p_ord-q_ord))

Solution (Python):
import itertools

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

r = sorted(list(itertools.permutations(p,n)))
a = r.index(p)
b = r.index(q)
print(abs(a-b))