program example
	implicit none

	integer(8) N,K
    
    read(*,*) N,K
    
    if(abs(N-K)>=N) then
    
    	write(*,*) N
        goto 1
    else
    	N=abs(N-K)
    
    end if
    
    if(K==1) then
    
    	write(*,*) 0
    
    end if
    
    do
    	if(abs(N-K)>=N) then
        	write(*,*) N
            exit
        else
        	N=abs(N-K)
        end if
    end do

1 end program example
