#include<stdio.h>
int main()
{
	int x, n, k, min;

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

	x = n;
	min = x;

	for (int i = 0; i < 10000000000000000000; i++) {
		if (x >= k) x = x - k;
		else        x = k - x;
		if (x < min) min = x;
	}

	printf("%d \n", min);

	return 0;
}