solve 0 k = 0
solve n k
    | n' < 0 && n'' < n = n''
    | n'' >= n = n
    | otherwise = solve n'' k
        where
            n' = n-k
            n'' = abs n'
main = do
    [n,k] <- map read . words <$> getLine :: IO [Integer]
    print $ solve (mod n k) k
