import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Data.List
import Data.Char


main :: IO ()
main = do
    (n,k) <- (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> BS.getLine
    let jj = n`mod`k
    print $ min jj (k-jj)



readInt :: BS.ByteString -> Maybe (Int,BS.ByteString)
readInt = BS.readInt . BS.dropWhile isSpace