Permutations with given distance

EOlymp - 101