#include <stdio.h>

int main (void) {
	int n, k, rest = 0;
	scanf("%d %d", &n, &k);
	
	rest = n % k;
	
	if (rest < k - rest) {
		printf("%d\n", rest);
	}
	else {
		printf("%d\n", k - rest);
	}
	return 0;
}
