{-# LANGUAGE BangPatterns, OverloadedStrings #-}

import Control.Monad
import Data.List
import Data.Bits
import Debug.Trace
import Data.Maybe
import qualified Data.ByteString.Char8 as C

main :: IO ()
main = do
  [n, k] <- readIntegers
  print $ solve n k

-- NがKで割り切れれば0になる
-- 4 7
-- 3 7
-- 4 7

-- 18 8
-- 10 8
-- 2 8
-- 6 8
-- 2 8

-- NをKで割った余りと、それをKから引いたもののうちの小さい方

solve n k = if modk < (k - modk) then modk else (k - modk)
  where
    modk = n `mod` k

-- 以下ライブラリ

readInts :: IO [Int]
readInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine

readNInts :: Int -> IO [[Int]]
readNInts = flip replicateM readInts

readIntegers :: IO [Integer]
readIntegers = map (fst . fromJust . C.readInteger) . C.words <$> C.getLine

readNIntegers :: Int -> IO [[Integer]]
readNIntegers = flip replicateM readIntegers

getNLns :: Int -> IO [String]
getNLns = flip replicateM getLine