program main
      implicit none
      integer(8) n, k
      integer(8) nmin

      read *, n,k
      nmin = n

      if ( k == 1 ) then
              nmin = 0
      else
        do
          n = abs(n-k)
          if ( n <= nmin ) then
                nmin = n
          else if ( n > nmin ) then
                exit
          end if
        end do
      end if
      print *, nmin

end program main