Working solution - needs an optimization.
first step to optimization - calculate distance to palindrom. permutations with the same distance are the same variables
Distance calculation.
Accepted version!