CF441D: Solve permutation prolem in graph view.