somebody pls explain the gennady approach masha and minions

link for code https://www.codechef.com/viewsolution/18921338

didn’t getting his approach , someone pls explain the logic

help @soham1234