program sample
    implicit none
    
    integer(8) :: a,b,c,i,j,m,n,k,a1,b1,c1
    integer(8), allocatable  ::x(:) 

    read(*,*) n,k
    if (n>k)then
        n=mod(n,k)
    end if
    
    a=k-n
    if (a<n)then
        write(*,*)a
    else
        write(*,*)n
    end if
    
    stop
end program sample
  

