#include <stdio.h>
#include <math.h>

int main()
{
	long int n,k,result,result1;
	
	scanf("%ld %ld",&n,&k);
	
	result = n;
	
	if(n % k == 0)
		result = 0;
	else
		while(1)
		{
			if(n > k)result %= k;
			if(n >= k / 2)
			{
				result = labs(n - k);
				break;
			}
			else 
			{
				break;	
			}
		}
	
	printf("%ld\n",result);
	
	return 0;
}