solution for problem 491