#include <stdio.h>

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