program example
	implicit none

	integer(8) N,K
    
    read(*,*) N,K

    if(mod(N,K)==0) then
        write(*,*) 0

    else if(mod(N,K)<dble(K)/2.0d0) then
        write(*,*) mod(N,K)

    else
        write(*,*) K-mod(N,K)
    end if


!    do
!    	if(abs(N-K)>=N) then
!        	write(*,*) N
!            exit
!        else
!        	N=abs(N-K)
!        end if
!    end do

end program example
