Ivan's Solution for SP task