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 as perm


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


N = int(input())
P = tuple(inpl())
Q = tuple(inpl())

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

for i, a in enumerate(A):
    
    if a == P:
        p = i
    if a == Q:
        q = i

print(abs(p - q))

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

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

import itertools
L = [int(''.join(_)) for _ in itertools.permutations(['1','2','3','4','5','6','7','8'][:N])]
L.sort()
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()))

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

Solution (Python):
import sys,math,string,bisect
from itertools import *
input=sys.stdin.readline
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
S=lambda :input().strip()
n=I()
i=input()

l=list(map(int,i.split()))
l=tuple(l)
i=input().strip()
m=list(map(int,i.split()))
m=tuple(m)
d=list(permutations(set(l)))

print(abs(d.index(l)-d.index(m)))

Solution (Python):
import itertools

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

m = [x for x in range(1, n+1)]
all = list(itertools.permutations(m))
a = all.index(p)
b = all.index(q)
print(abs(a - b))

Solution (Python):
from itertools import permutations
n = int(input())
ps = ''.join(input().split())
qs = ''.join(input().split())
perm = sorted(
    list(map(''.join, (permutations(map(str, list(range(1, n+1))))))))
pi, qi = 0, 0


for i in range(len(perm)):
    
    if perm[i] == ps:
        
        pi = i+1
    if perm[i] == qs:
        
        qi = i+1

print(abs(pi-qi))

Solution (Python):
import itertools

def resolve():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    l = list(itertools.permutations(range(1, N+1)))
    p_idx = l.index(P)
    q_idx = l.index(Q)
    print(abs(p_idx - q_idx))

if __name__ == "__main__":
    resolve()

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

import itertools
arr = list(map(lambda x: ('').join(list(map(str,x))),list(itertools.permutations(range(1,N+1)))))

print(abs(arr.index(('').join(P))-arr.index(('').join(Q))))

Solution (Python):

import itertools

N = int(input().split()[0])
p_list = list(input().split())
q_list = list(input().split())

pp = itertools.permutations(p_list, N)
pp = list(sorted(["".join(x) for x in pp]))

qp = itertools.permutations(q_list, N)
qp = list(sorted(["".join(x) for x in qp]))

p = "".join(p_list)
q = "".join(q_list)

p_index = qp.index(p)
q_index = qp.index(q)

ans = abs(p_index - q_index)
print(ans)

Solution (Python):
import itertools

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

nP = 0
nQ = 0
for i, p in enumerate(itertools.permutations(list(range(1, N+1)))):
  if p == P:
    nP = i
  if p == Q:
    nQ = i
    
print(abs(nP - nQ))

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

Solution (Python):
def examA():
    K, X = LI()
    if K*500>=X:
        ans = "Yes"
    else:
        ans = "No"
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    ans = 0
    for i in range(N-2):
        if S[i:i+3]=="ABC":
            ans +=1
    print(ans)
    return

def examC():
    N = I()
    P = LI()
    Q = LI()
    Cp = [i for i in itertools.permutations(P,N)]; Cp.sort()
    Cq = [i for i in itertools.permutations(Q, N)]; Cq.sort()


    for k,i in enumerate(Cp):
        flag = True
        for j in range(N):
            if P[j]!=i[j]:
                flag = False
                break
        if flag:
            a = k
            break
    for k,i in enumerate(Cq):
        flag = True
        for j in range(N):
            if Q[j]!=i[j]:
                flag = False
                break
        if flag:
            b = k
            break
    ans = abs(a-b)
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

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

li = list(itertools.permutations(range(1, n+1), n))
a = li.index(p)
b = li.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()))

J = [i for i in range(1, N+1)]
JJ = list(itertools.permutations(J))
for i in range(len(JJ)):
    if P == JJ[i]:
        pp = i
    if Q == JJ[i]:
        qq = i
print(abs(pp - qq))

Solution (Python):

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

def f(n, cur=[]):
    if len(cur) == n:
        yield cur
    else:
        for i in range(1, n + 1):
            if i not in cur:
                _next = cur[:]
                _next.append(i)
                yield from f(n, _next)


idx = 1
pflag, qflag = False, False
for g in f(n):
    if p == g:
        pflag = idx
    if q == g:
        qflag = idx
    if pflag and qflag:
        break
    idx += 1
print(abs(pflag - qflag))

Solution (Python):
import itertools
import copy
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
l = copy.deepcopy(p)
l.sort()
cnt = 0
p_c = 0
q_c = 0
p = tuple(p)
q = tuple(q)
for i in itertools.permutations(l):
    if i==p:
        p_c = cnt
    if i==q:
        q_c = cnt
    cnt+=1
print(abs(p_c-q_c))

Solution (Python):
N=int(input())
Pn=tuple(map(int, input().split()))
Qn=tuple(map(int, input().split()))
a,b=0,0

from itertools import permutations
seq = [i for i in range(1, N+1)]
for i, l in enumerate(list(permutations(seq))):
    if l == Pn:
        a = i
    if l == Qn:
        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()))

X = list(itertools.permutations(P))
X.sort()
for i,x in enumerate(X):
	if x == P:
		a = i
	if x == 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()))

nlist = list(range(1, n+1))
nmlist = []

for val in itertools.permutations(nlist, n):
    nmlist.append(val)

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

Solution (Python):
def pro(x):
    ret = 1
    for i in range(x):
        ret *= i+1
    return ret


def cal(n, da):
    ju = [False for i in range(n+1)]
    ret = 1
    for i in range(n):
        now, co = 1, 0
        while(now != da[i]):
            if not ju[now]:
                co += 1
            now += 1
        
        ret += pro(n-1-i) * co
        ju[da[i]] = True
    
    return ret

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

print(abs(cal(n,p)-cal(n,q)))

Solution (Python):

from itertools import permutations
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
if p==q:
    print(0)
else:
    p, q = tuple(p), tuple(q)
    l = [i for i in range(1, n+1)]
    flag = False
    ss = 0
    for i, each in enumerate(permutations(l)):
        if each == p or each == q:
            if flag:
                print(abs(ss - i))
            else:
                flag = True
                ss = i

Solution (Python):
import itertools
n=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))
arr=[i+1 for i in range(n)]
arr2=list(itertools.permutations(arr))
for num,i in enumerate(arr2):
    if A==i:
        a=num+1
    if B==i:
        b=num+1
print(abs(a-b))

Solution (Python):
import itertools

N = int(input())
P = tuple([int(i) for i in input().split()][0:N])
Q = tuple([int(i) for i in input().split()][0:N])
l = list(itertools.permutations(range(1,N+1)))
print(abs(l.index(Q) - l.index(P)))

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

from itertools import permutations

ans = 0
xs = permutations(range(1, n+1), n)
xs = sorted(xs)


for i, x in enumerate(xs):
    if x == p:
        pi = i
    if x == q:
        qi = i

print(abs(pi - qi))

Solution (Python):
from itertools import permutations
import math

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

orders = dict()
i = 1
for perms in sorted(permutations(range(1, N+1))):
    orders[perms] = i
    i += 1

print(int(math.fabs(orders[P] - orders[Q])))

Solution (Python):
import itertools
n = int(input())
p = list(map(int, input().split()))
pp = "".join((str(n) for n in p))
q = list(map(int, input().split()))
qq = "".join((str(n) for n in q))
a = [i+1 for i in range(n)]
b = 0
c = 0
d = 0
for i in itertools.permutations(a):
	d += 1
	x = "".join((str(n) for n in i))
	if pp == x:
		b = d
	if qq == x:
		c = d
print (abs(b-c))

Solution (Python):
import math
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

A=1
for i in range(n):
    tmp=a[i]
    for j in range(i):
        if a[j]<a[i]: tmp-=1 
    A+=math.factorial(n-i-1)*(tmp-1)

B=1
for i in range(n):
    tmp=b[i]
    for j in range(i):
        if b[j]<b[i]: tmp-=1 
    B+=math.factorial(n-i-1)*(tmp-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()))

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

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

import itertools

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

for i, x in enumerate(l):
    x = list(x)

    if x == P:
        p = i

    if x == Q:
        q = i


print(abs(p - q))

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

from bisect import bisect
from math import factorial
def rank(l):
  r = 0
  nums = [_ for _ in range(1,n+1)]
  for i in range(n):
    r += bisect(nums, l[i]) * factorial(len(nums)-1)
    nums.remove(l[i])
  return r
print(abs(rank(p)-rank(q)))

Solution (Python):
import sys
import itertools
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
li=[]
for i in range(n):
    li.append(i+1)
a=list(itertools.permutations(li))
tmp=[0,0]

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

Solution (Python):
import itertools

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

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

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

Solution (Python):
from itertools import permutations

N=int(input())
l=[i+1 for i in range(N)]
P=tuple(map(int, input().split()))
Q=tuple(map(int, input().split()))
counter=0
for t in permutations(l):
  if P==t:
    a=counter
    
  if Q==t:
    b=counter
    
  counter+=1
  
print(abs(a-b))

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

Solution (Python):
from itertools import permutations

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


A = ""
for i in range(1, N + 1):
  A += str(i)
A = list(permutations(A, N))
A.sort()


B = []
for i in range(len(A)):
  B.append("".join(A[i]))

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

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

import itertools

idx = 1
for l in itertools.permutations(range(1, N+1)):
    if l == P:
        a = idx
    if l == Q:
        b = idx

    idx += 1

print(abs(a-b))

Solution (Python):
import sys
from itertools import permutations
input = sys.stdin.readline
n = int(input())
p = int(''.join([x for x in input().strip().split()]))
q = int(''.join([x for x in input().strip().split()]))
d = [str(i) for i in range(1, n+1)]
l = sorted([int(''.join(i)) for i in permutations(d)])
print(abs(l.index(p)-l.index(q)))

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

import itertools

per = list(itertools.permutations(range(1, N+1)))
for idx, ele in enumerate(per):
    if ele == P:
        s = idx
    if ele == Q:
        t = idx

print(abs(s-t))

Solution (Python):
import itertools
n = int(input())
a = tuple(i for i in range(1, n+1))
x = tuple(map(int, input().split()))
y = tuple(map(int, input().split()))

for i, z in enumerate(tuple(itertools.permutations(a))):
    if z == x:
        x_count = i
    if z == y:
        y_count = i
print(abs(x_count - y_count))

Solution (Python):
import unittest
import itertools


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([1, 3, 2], [3, 1, 2]), 3)

    def test_2(self):
        self.assertEqual(think([7, 3, 5, 4, 2, 1, 6, 8], [3, 8, 2, 5, 4, 6, 7, 1]), 17517)

    def test_3(self):
        self.assertEqual(think([1, 2, 3], [1, 2, 3]), 0)


def solve():
    p, q = read()
    result = think(p, q)
    write(result)


def read():
    n = read_int(1)[0]
    p = read_int(n)
    q = read_int(n)
    return p, q


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(p, q):
    base = list(p)
    base.sort()
    index_of_p = 0
    index_of_q = 0
    for i, perm in enumerate(itertools.permutations(base)):
        if perm == tuple(p):
            index_of_p = i
        if perm == tuple(q):
            index_of_q = i
    return abs(index_of_p - index_of_q)


def write(result):
    print(result)


if __name__ == '__main__':
    
    solve()

Solution (Python):
import math
import queue
import bisect
import heapq
import time
import itertools

mod = int(1e9+7)

def swap(a,b):
    return (b,a)

def gcd(a,b): 
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def lcm(a,b): 
    return a/gcd(a,b)*b

def divisors(a): 
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def is_prime(a): 
    if a<2:
        return False
    elif a==2:
        return True
    elif a%2==0:
        return False
    sqrt_num = int(a**0.5)
    for i in range(3,sqrt_num+1,2):
        if a%i==0:
            return False
    return True

def prime_num(a): 
    pn = [2]
    for i in range(3,int(a**0.5),2):
        prime = True
        for j in pn:
            if i%j==0:
                prime = False
                break
        if prime:
            pn.append(i)
    return pn

def prime_fact(a): 
    sqrt = math.sqrt(a)
    res = []
    i = 2
    if is_prime(a):
        res.append(a)
    else:
        while a!=1:
            while a%i==0:
                res.append(i)
                a //= i
            i += 1
    return res

def main():
    n = int(input())
    p = [x for x in map(int,input().split())]
    q = [x for x in map(int,input().split())]
    if p == q:
        print(0)
        return
    nums = [i+1 for i in range(n)]
    cnt = 1
    l = 0
    h = 0
    for x in itertools.permutations(nums):
        y = [z for z in x]
        if y == p:
            l = cnt
        elif y == q:
            h = cnt
        cnt += 1
    print(abs(l-h))
    return

if __name__=='__main__':
    main()

Solution (Python):
import itertools

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

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

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

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

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

a = 0
b = 0

for i, p in enumerate(perm) :
  if p == P :
    a = i + 1
  if p == Q :
    b = i + 1
    
print(abs(a - b))

Solution (Python):
from itertools import permutations

n = int(input())
ppp = tuple(map(int, input().split()))
qqq = tuple(map(int, input().split()))
a, b = -1, -1
for i, rrr in enumerate(permutations(range(1, n + 1))):
    if a == -1 and ppp == rrr:
        a = i
    if b == -1 and qqq == rrr:
        b = i
    if a != -1 and b != -1:
        break
print(abs(a - b))

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

list = list(itertools.permutations([i+1 for i in range(n)]))
a = 0
b = 0
for i in range(len(list)) :
    if list[i] == p:
        a = i + 1
    if list[i] == q:
        b = i + 1


print(abs(a-b))

Solution (Python):
import math

n = int(input())
ps = list(map(int, input().split(' ')))
qs = list(map(int, input().split(' ')))

def func(array):
    sort_array = sorted(array)
    head = array[0]
    index = sort_array.index(head)
    num_pattern = math.factorial(len(array) - 1)
    return index * num_pattern

p_cnt = 1
for i in range(len(ps)):
    array = ps[i:]
    p_cnt += func(array)

q_cnt = 1
for i in range(len(qs)):
    array = qs[i:]
    q_cnt += func(array)
print(abs(p_cnt - q_cnt))

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

a = [x for x in range(1, n+1)]

import itertools

b = list(itertools.permutations(a))
for num, x in enumerate(b):
    if x == p:
        res1 = num
    if x == q:
        res2 = num
print(abs(res1 - res2))

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

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

cnt = 1
a = 0
b = 0
for tpl in itertools.permutations(range(1,N+1)):
    lst = list(tpl)
    if lst == P:
        a = cnt
    if lst == Q:
        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()))

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

Solution (Python):
import sys
from pprint import pprint
import itertools

def solve(n, p, q):
    patterns = list(itertools.permutations(range(1, n+1)))
    
    print(abs(patterns.index(p) - patterns.index(q)))


if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    p = tuple(map(int, sys.stdin.readline().strip().split(" ")))
    q = tuple(map(int, sys.stdin.readline().strip().split(" ")))
    solve(n, p, q)

Solution (Python):
n = int(input())
xn = list(map(int, input().split()))
yn = list(map(int, input().split()))

fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320]
def rank(an):
  used = []
  n = len(an)
  rank = 1
  for i, a in enumerate(an):
    b = a - len([c for c in used if c < a]) - 1
    rank += fact[n-i-1] * b
    used.append(a)
  return rank

print(abs(rank(xn) - rank(yn)))

Solution (Python):
import itertools
n = int(input())
a_ = list(map(int, input().split()))
q_ = list(map(int, input().split()))
a = ""
q = ""
for i in range(n):
    a += str(a_[i])
    q += str(q_[i])

l = list(map(str, range(1, n+1)))
p = itertools.permutations(l, n)

d_ = []
for v in p:
    str_ = ""
    for c in v:
        str_ += str(c)
    d_.append(str_)
d_.sort()

dict_ = {}
for i, v in enumerate(d_):
    dict_[v] = i + 1

print(abs(dict_[a] - dict_[q]))

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


ans=0
for idx,e in enumerate(P,1):
    if e==p:
        ans+=idx
    if e==q:
        ans-=idx
    
print(abs(ans))

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)))
L=len(A)
X=[]
for l in range(L):
    if A[l]==P or A[l]==Q:
        X.append(l)
print(max(X)-min(X))

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

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

s = list(itertools.permutations(lst))
a,b = 0,0
for i in range(len(s)):
    if s[i] == P:
        a = i
    if s[i] == Q:
        b = i

print(abs(a-b))

Solution (Python):
from sys import stdin
import numpy as np
import itertools




def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    Q = [int(x) for x in input().split()]
    p = 0
    q = 0
    i = 0
    for x in itertools.permutations(list(range(1,N+1))):
        i += 1
        x = list(x)
        if P == x:
            p = i
        if Q == x:
            q = i
    print(abs(p - q))
    return


if __name__ == "__main__":
    main()

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

a = 0
als = list(range(1,n+1))
for i in range(n):
    for j in range(len(als)):
        if als[j] == p[i]:
            als.pop(j)
            break
    a += (j)*factorial(n-1-i)

b = 0
bls = list(range(1,n+1))
for i in range(n):
    for j in range(len(bls)):
        if bls[j] == q[i]:
            bls.pop(j)
            break
    b += (j)*factorial(n-1-i)

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 = 0
b = 0
for i, r in enumerate(permutations(range(n), n)):
    flagp = 1
    flagq = 1
    for j in range(n):
        if p[j]-1 != r[j]:
            flagp = 0
        if q[j]-1 != r[j]:
            flagq = 0
    if flagp:
        a = i
    if flagq:
        b = i

print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
L = list(range(1, n + 1))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
junretu = list(itertools.permutations(L))
junretu.sort()
a, b = 0, 0
for i, jun in enumerate(junretu):
    if P == jun:
        a = i
    if Q == jun:
        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()))

p = 0
q = 0
L = [i for i in range(1, N+1)]
for i, v in enumerate(permutations(L), 1):
  if v == P:
    p = i
  if v == Q:
    q = i
    
print(abs(p - q))

Solution (Python):
from itertools import permutations

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

vec = [x for x in range(1,n+1)]
pmt = permutations(vec, n)
a = 0
b = 0
for i, v in enumerate(pmt):
    if list(v) == P:
        a = i + 1
    if list(v) == Q:
        b = i + 1

    if a > 0 and b > 0:
        break

print(abs(a - b))

Solution (Python):
import itertools
tmp=[]
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
for i in range(1,N+1):
  tmp.append(i)
tmp=list(itertools.permutations(tmp))
A=0
B=0
for i,seq in enumerate(tmp):
  if list(seq)==P:
    A=i
  if list(seq)==Q:
    B=i
print(abs(A-B))

Solution (Python):
import itertools

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

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

print(abs(sum_list.index(p_list)-sum_list.index(q_list)))

Solution (Python):
import itertools
n = int(input())
l = [i + 1 for i in range(n)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a = 0
b = 0
cnt = 1
for v in itertools.permutations(l, n):
    for i in range(n):
        if v[i] != p[i]:
            break
        if i == n - 1:
            a = cnt
    for i in range(n):
        if v[i] != q[i]:
            break
        if i == n - 1:
            b = cnt
    cnt += 1
print(abs(a - b))

Solution (Python):
import sys
import itertools

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

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

for i,x in enumerate(L):
    if x==P: a=i
    if x==Q: b=i

print abs(a-b)

Solution (Python):
import itertools

n = int(input())
p = tuple(int(s) for s in input().split())
q = tuple(int(s) for s in input().split())
seq = []

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

b = list(itertools.permutations(seq))

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, b = 1, 1
frac = 1
for i in range(N - 1):
  frac *= (i + 1)
for i in range(N):
  a += (A[i] - 1) * frac
  b += (B[i] - 1) * frac
  if i != N - 1:
    for j in range(i + 1, N):
      if A[j] > A[i]:
        A[j] -= 1
      if B[j] > B[i]:
        B[j] -= 1
    frac //= (N - 1 - i)
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 = 0
b = 0
flagP = False
flagQ = False
count = 0
for x in permutations(range(1, N+1)):
    count += 1
    if flagP == False:
        for i in range(N):
            if x[i] != P[i]:
                break
            elif i == N-1:
                flagP = True
                a = count
    if flagQ == False:
        for i in range(N):
            if x[i] != Q[i]:
                break
            elif i == N-1:
                flagQ = True
                b = count

    if flagQ and flagP:
        break


print(abs(a-b))

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

a = 0
b = 0
for i, j in enumerate(list(itertools.permutations(range(1, n+1)))):
    j = list(j)
    if j == p:
        a = i
    if j == q:
        b = i

print(abs(a-b))

Solution (Python):
import sys
import itertools

N = int(input())
l = [x+1 for x in range(N)]
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
if P == Q:
    print(0)
    sys.exit()

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

Solution (Python):
import itertools

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

C = list(itertools.permutations(R, N))

a = 0
b = 0
for i in range(len(C)):
    if list(C[i]) == P: a = i
    if list(C[i]) == Q: b = i

print(abs(a-b))

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


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

p = itertools.permutations(l, n)
ans_a = 0
ans_b = 0
count = 0

for i in p:
    if i == a:
        ans_a = count
    if i == b:
        ans_b = count
    count += 1

print(abs(ans_b-ans_a))

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

import itertools
all=[]
seq=list(range(1,n+1))
for ptn in itertools.permutations(seq):
  all.append(list(ptn))

ans=abs(all.index(p)-all.index(q))

print(ans)

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
L = [i+1 for i in range(N)]
R = [i+1 for i in range(N)]
A,B = 0,0
for i in range(N-1):
    A *= N-i
    B *= N-i
    A += L.index(P[i])
    B += R.index(Q[i])
    L[L.index(P[i])] = 100
    R[R.index(Q[i])] = 100
    L.sort()
    R.sort()
print(abs(A-B))

Solution (Python):
from itertools import permutations
n=int(input())
num=[str(i+1) for i in range(n)]
nums=sorted([int("".join(i)) for i in permutations(num,n)])
P=int("".join(map(str,input().split())))
Q=int("".join(map(str,input().split())))
a,b=0,0
for j,i in enumerate(nums):
    if i==P:a=j
    if i==Q:b=j
print(abs(a-b))

Solution (Python):

from itertools import permutations
N = int(input())
P = list(map(str, input().split()))
Q = list(map(str, input().split()))

l = list(permutations(P))
l.sort()
ll = []
for i in range(len(l)):
    a = list(map(str, l[i]))
    ll.append("".join(a))
p_idx = ll.index("".join(P))
q_idx = ll.index("".join(Q))
print(abs(p_idx - q_idx))

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



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

print(abs(a-b))