import Data.List.Split

toInt :: String -> Int
toInt = read :: String -> Int

main = do
  s <- getLine
  let l = map toInt $ splitOn " " s
  let n = l!!0
  let k = l!!1
  let a = mod n k
  putStrLn $ show $ min a (k-a)
