program main
    implicit none
    integer n,k
    integer, allocatable, dimension(:) :: a
    integer i,d,min
    read(*,*) n,k

    d = mod(n,k)
    min = d
    
    do i = 1,100000
        if(min == 0) exit
        d = abs(d-k)
        if(d < min) then
            min = d
        end if
    end do
    write(*,*) min


end program