Divisible Subarrays (Pigeonhole Principle)

Sir, in the pigeonhole problem, if I want to find the mod of a negative no. , how can I achieve that? In the lecture, it was said sum%=n; sum=(sum+n)%n; where sum is the number whose mod is to be found out. But if I take a number let’s say -17 and n to be 5, after execution of first statement , sum becomes -2 and after that sum becomes 3 but the answer should be 2

Plz help.