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

int main(void) {
	long long int n, k;
	long long int num, pre_num;

	scanf("%lld%lld", &n, &k);

	num = n % k;

	pre_num = abs(num - k);

	if (pre_num < num)
		num = pre_num;

	printf("%lld", num);

	return 0;
}

