main = do 
 [n,k] <- map (read::String->Int) .words <$> getLine
 --let arr = dropWhile (k<).zipWith ($) (map (f (\x -> abs(k - x) )) $[0..] ) $repeat n
 let diff = n `mod` k
 --print .minimum $[arr!!0,arr!!1]
 print .minimum $[diff, k-diff]
f :: (a->a) -> Int  -> (a->a)
f g 0 = id
f g m = g . f g (m-1) 