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

int main(void) {
	long n, k;
	scanf("%ld %ld", &n, &k);
	long min = fmax(n, k);
	while (1)
	{
		n = n % k;
		if (min == n) {
			break;
		}
		min = n;
	}
	printf("%d", min<abs(min-k)?min: abs(min - k));

	return 0;
}
