I think that you are on the wrong track with using mod.
It seems to me that this is a variation to a "greedy algorithm" problem, normaly associated with working out efficient change with coins in homework questions, but I think this seems to be a variant of it.
Wikipedia has a page on it, and I am sure googling greedy algorithm will help you out.