Optimize problem 51