      PROGRAM replacingInteger
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16) :: numK
      
      read*,n,k
      
      numK = n/K
      
      n = n - numK*k
      
      print*,min( n,abs(n-k) )
      
      END PROGRAM