problem 23 (need to optimize in the future)