Solution for the first problem