find the smallest partial sum modulo P that is at least K.

Here is the link to the problem-: https://www.codechef.com/problems/BESTSUM
I am trying to understand the concept used in the Submissions. I am not able to get it. Need help!

//