Brute force solution to problem 2