def mod_min(a, b):
    """
    return r such that r = a (mod b), with minimum |r|
    """
    # like divmod_min, just skipping a single add
    r = (a % b)
    diff = b - r
    if abs(r) > abs(diff):
        r = -diff
    return r