import Control.Monad
import Data.List

ans n k = 
  if amari > min  then min else amari
  where
    amari = n `mod` k
    min = abs $ amari - k

main :: IO ()
main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  print $ ans n k