brute force for problem 5
brute force, still some optimizations to apply