A better solution on the base of solveKami.