Solution for problem 24. solved by getting all permutations in order.