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

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


def compare(a, b):
    for i in range(n):
        if a[i] != b[i]:
            return False
    return True


idx_p = -1
idx_q = -1
for i, x in enumerate(permutations(range(n))):
    if compare(p, x):
        idx_p = i
    if compare(q, x):
        idx_q = i

ans = abs(idx_p - idx_q)
print(ans)

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

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

B.sort()



p=-1
q=-1
for i in range(len(B)):
    
    if P == [j for j in B[i]]:
        
        p=i
    if Q== [j for j in B[i]]:
        q=i
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 for i in range(1,n+1)]
x=list(list(itertools.permutations(l,n)))

a=0
b=0
for i in range(len(x)):
    if p==list(x[i]):
        a=i+1
    if q==list(x[i]):
        b=i+1
print(abs(a-b))

Solution (Python):


import sys
from itertools import permutations

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

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

Solution (Python):
N = int(input())
import itertools
all = list(itertools.permutations([i+1 for i in range(N)],N))
lst1 = tuple(map(int, input().split()))
lst2 = tuple(map(int, input().split()))
id1 = 0
id2 = 0
for i in range(len(all)):
    if all[i] == lst1:
        id1 = i
    if all[i] == lst2:
        id2 = i
print(abs(id1-id2))

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

Solution (Python):
import sys
from itertools import permutations
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    P=tuple(map(int,input().split()))
    Q=tuple(map(int,input().split()))
    a=-1
    for i,x in enumerate(permutations(range(1,N+1))):
        if x==P or x==Q:
            if a==-1:
                a=i
            else:
                print(abs(i-a))
                exit()
    print(0)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import permutations as permi
input = sys.stdin.readline
N = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
t = []
a = 0
b = 0
for pm in permi([x for x in range(1, N + 1)], N): t.append(pm)
for i in range(len(t)):
  if p == t[i]: a = i
  if q == t[i]: b = i 
print(abs(a - b))

Solution (Python):
import itertools


N = int(input())
P = tuple(int(i) for i in input().split())
Q = tuple(int(i) for i in input().split())


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

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

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

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

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

main()

Solution (Python):
import itertools

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

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

  if P == p:
    p_pos = i

  if Q == p:
    q_pos = i
  i += 1
print(abs(p_pos - q_pos))

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

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

    a = list(itertools.permutations([i for i in range(1, N+1)]))
    k, l = 0, 0
    for i in range(len(a)):
        if list(a[i]) == P: k = i
        if list(a[i]) == Q: l = i
    return abs(k-l)

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

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

for i,t in enumerate(itertools.permutations(tuple(list(range(1,N+1))), N)):
    if t == P:
        i_p = i
    if t == Q:
        i_q = i

print(abs(i_p-i_q))

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

nums = [i+1 for i in range(N)]
import itertools
count = 0
for v in itertools.permutations(nums,N):
  count += 1
  if v == P:
    a = count
  if v == Q:
    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()))
M = [ i for i in range(1,N+1)]
pp = list(itertools.permutations(M,N))
print(abs(pp.index(P)-pp.index(Q)))

Solution (Python):
import itertools

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n = inp()
    p = tuple(inpl())
    q = tuple(inpl())
    pre = []
    for i in permutations(range(1,n+1)):
        pre.append(i)
    pre.sort()
    x,y = 0,0
    for i in range(len(pre)):
        if pre[i] == p:
            x = i
        if pre[i] == q:
            y = i
    print(abs(x-y))

if __name__ == "__main__":
    main()

Solution (Python):
'''input
3
1 3 2
3 1 2
'''

from sys import stdin, stdout
import gc
from copy import deepcopy
from itertools import permutations
gc.disable()
input = stdin.readline

	

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

a = list(permutations(P))
a.sort()
for i in range(len(a)):
	if tuple(P) == a[i]:
		first = i 
	if tuple(Q) == a[i]:
		second = i 

print(abs(first - second))

Solution (Python):
from itertools import permutations
from operator import eq
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
for i, c in enumerate(permutations(range(1, N+1), N)):
    if all(map(eq, c, P)):
        a = i
    if all(map(eq, c, Q)):
        b = i
print(abs(a-b))

Solution (Python):
import sys
from itertools import permutations
n = int(input())
ls = ["".join([str(j) for j in list(i)]) for i in permutations(range(1,n+1))]
p_ls = "".join([i for i in sys.stdin.readline().split()])
q_ls = "".join([i for i in sys.stdin.readline().split()])
print(abs(ls.index(p_ls) - ls.index(q_ls)))

Solution (Python):
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
from itertools import permutations
s = list(permutations([i for i in range(1,N+1)]))
for i,e in enumerate(s):
    if e == P:
        p_ans = i
    if e == Q:
        q_ans = i
print(abs(p_ans-q_ans))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import permutations as perm
n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
lst = list(perm(list(range(1, n+1)),n))
lst.sort()
pn=qn=0
for i,j in enumerate(lst, 1):
  if p == j:
    pn = i
  if q == j:
    qn = i
  if pn*qn!=0:
    break
print(abs(pn-qn))

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

import itertools

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

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)]
i = 0
p = 0
q = 0

for v in itertools.permutations(l, N):
    if(P == list(v)):
        p = i
    if(Q == list(v)):
        q = i
    i += 1

print(abs(p - q))

Solution (Python):
n=int(input())
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
l=list(range(1,n+1)[::-1])
import itertools
ll=[]
for x in itertools.permutations(l):
    ll.append(list(x))
ll.sort()
a=0
b=0
c=0
for i in ll:
    if i==p:
        a=c
    if i==q:
        b=c
    c+=1
print(abs(a-b))

Solution (Python):
from itertools import permutations
n = int(input())
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
cnta = 0
cntb = 0
for x in permutations(range(1,n+1)):
  if a == x:
    aa = cnta
  if b == x:
    bb = cntb
  cnta += 1
  cntb += 1
print(abs(aa-bb))

Solution (Python):
from itertools import permutations

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

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

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

a = 0
b = 0

for i, x in enumerate(permutations(d)):
    if x == p:
        a = i
    if x == q:
        b = i

print(abs(a - b))

Solution (Python):
import itertools
n=int(input())
x=list(itertools.permutations([i+1 for i in range(n)]))
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
l=len(x)
a,b=-1,-1
for i in range(l):
    
    if list(x[i])==p:
        a=i
    if list(x[i])==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()))

junretsu = []
for i in itertools.permutations(range(1, n + 1)):
    junretsu.append(i)
junretsu = sorted(junretsu)
a = junretsu.index(p)
b = junretsu.index(q)

print(abs(a - b))

Solution (Python):
import sys
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = tuple(map(int, readline().split()))
    b = tuple(map(int, readline().split()))
    nl = sorted(a)
    an = -1
    bn = -1
    for i, v in enumerate(itertools.permutations(nl, n)):
        if v == a:
            an = i
        if v == b:
            bn = i
    print(abs(an - bn))


if __name__ == '__main__':
    solve()

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

perms = sorted([p for p in permutations(range(1, N + 1))])
print(abs(perms.index(P) - perms.index(Q)))

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

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
q = LI()

cnt = 0
for i in permutations(range(1,n+1)):
    if list(i) == p:
        x = cnt
    if list(i) == q:
        y = cnt
    cnt += 1
print(abs(x-y))

Solution (Python):
from itertools import permutations

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

d = dict()
for i, perm in enumerate(permutations(range(1, n + 1)), 1):
    d[perm] = i

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

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

from itertools import permutations

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

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

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

Solution (Python):
import itertools

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

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

for idx, i in enumerate(itertools.permutations(N_list)):
    
    if list(i) == P:
        p = idx
    if list(i) == Q:
        q = idx

print(abs(p-q))

Solution (Python):
import itertools

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

A = list(range(1, N + 1))
for i, x in enumerate(itertools.permutations(A)):
    if list(x) == P:
        pn = i
    if list(x) == Q:
        qn = i
print(abs(pn - qn))

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

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

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

def count(x):
    i = 0
    for perm in permutations(range(1, n+1)):
        if perm == x:
            return i
        i += 1

print(abs(count(p) - count(q)))

Solution (Python):
from itertools import permutations

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
l = list(range(1, N+1))
cnt = 0

for p in permutations(l):
    if list(p)==P:
        a = cnt
    
    if list(p)==Q:
        b = cnt
    
    cnt += 1
    
print(abs(a-b))

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, P, Q):
    for i, R in enumerate(permutations(range(1, N+1), r=N)):
        R = list(R)
        for a, r in zip(P, R):
            if a != r:
                break
        else:
            p = i
        for a, r in zip(Q, R):
            if a != r:
                break
        else:
            q = i

    return abs(p-q)


def main():
    N = read_int()
    P = read_int_n()
    Q = read_int_n()
    print(slv(N, P, Q))


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations as perm

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
l = list(range(1, n + 1))

def f(p, q):
    for i, j in zip(p, q):
        if i != j:
            return False
    return True

aa,bb = -1,-1
for i, p in enumerate(perm(l)):
    if f(p, a):
        aa = i
    if f(p, b):
        bb = i
print(abs(aa - bb))

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


for i,c in enumerate(C):
    if list(c)==list(P):
        f=i
    if list(c)==Q:
        s=i
print(abs(s-f))

Solution (Python):
def c_count_order():
    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())

    rank = {perm: k for k, perm in enumerate(permutations(range(1, N + 1), N), 1)}
    return abs(rank[P] - rank[Q])

print(c_count_order())

Solution (Python):
n=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
from itertools import permutations
jun = list(permutations(range(1,n+1),n))
ansp=-1
ansq=-1
for a,b in enumerate(jun):
    if b==P:
        ansp=a
    if b==Q:
        ansq=a
print(abs(ansp-ansq))

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

import itertools
z = [list(per) for per in itertools.permutations(range(1, n + 1), r=n)]
print(abs(z.index(p) - z.index(q)))

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

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
from itertools import permutations
ans = []
for i,r in enumerate(permutations(range(1,n+1))):
    if all(p[j]==r[j] for j in range(n)):
        ans.append(i)
    if all(q[j]==r[j] for j in range(n)):
        ans.append(i)
ans = abs(ans[0]-ans[1])
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
p = inpl()
q = inpl()
p = tuple(p)
q = tuple(q)

a = 0
b = 0
for i,num in enumerate(itertools.permutations(range(1,n+1))):
    if p == num:
        a = i+1
    if q == num:
        b = i+1
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()))
    seq = list(range(1,n+1))
    l = sorted(list(itertools.permutations(seq)))
    d = {}
    for j in range(len(l)):
        d[tuple(l[j])] = j+1
    print(abs(d[p]-d[q]))
    
if __name__ == '__main__':
    main()

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

from itertools import permutations
l = []
for pp in permutations(range(1, n+1)):
    ll = []
    for _p in pp:
        ll.append(_p)
    l.append(str(ll))

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

Solution (Python):
import itertools
N=int(input())
P=tuple([int(i)-1 for i in input().split()])
Q=tuple([int(i)-1 for i in input().split()])
SEQ=sorted(list(itertools.permutations(range(N),N)))


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

Solution (Python):
N = int(input())
P1 = tuple(map(int, input().split()))
P2 = tuple(map(int, input().split()))

import itertools
a = 0
b = 0
for perm in itertools.permutations([i+1 for i in range(N)]):
    if perm <= P1:
        a += 1
    if perm <= P2:
        b += 1
print(abs(b - a))

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

def kai(x):
    a = 1
    for i in range(1, x + 1):
        a *= i
    return a

al= list(range(1,n+1))
j = n-1
a = 0
for i in p:
    a += al.index(i) * kai(j)
    j -= 1
    al.remove(i)

al = list(range(1, n+1))
j = n-1
b = 0
for i in q:
    b += al.index(i) * kai(j)
    j -= 1
    al.remove(i)

print(abs(a-b))

Solution (Python):
from itertools import permutations
n = input()
l1 = [int(i) for i in raw_input().split(" ")]
l2 = [int(i) for i in raw_input().split(" ")]
per = list(permutations(range(1,n+1)))
a = 0
b = 0
cnt = 0;
for i in per:
	
	i = list(i)
	if(i == l1): a = cnt
	if(i == l2): b=cnt
	cnt+=1;
print abs(a-b)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    P=LI()
    Q=LI()

    a = 0
    b = 0
    for c,it in enumerate(permutations([i+1 for i in range(N)])):
        if P==list(it):
            a = c
        if Q==list(it):
            b = c
    print(abs(a-b))

if __name__ == '__main__':
    main()

Solution (Python):


import sys
from itertools import permutations
sys.setrecursionlimit(300000)


def solve(N: int, P: "List[int]", Q: "List[int]"):
    def equals(x, y):
        for i in range(len(x)):
            if x[i] != y[i] - 1:
                return False
        return True
    perm = list(permutations(list(range(N))))
    a = 0
    b = 0
    for i, p in enumerate(perm):
        if equals(p, P):
            a = i
        if equals(p, Q):
            b = i
    print(abs(a - b))
    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()