import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import qualified Data.IntMap as IM
import Data.List
import Control.Monad

readInput :: IO [Int]
readInput = map (either error fst . T.decimal) . T.words <$> T.getLine

solve x k
  | x' < x = solve x' k
  | otherwise = x
  where
    x' = abs (x - k * mult) `min` abs (x - k * succ mult)
    mult = x `div` k

main = do
  [n, k] <- readInput
  print $ solve n k