#include <stdio.h>
#include <inttypes.h>

int main(void) {
	uint64_t N, K;
	uint64_t asumi, iguti;
	if (scanf("%" SCNu64 "%" SCNu64, &N, &K) != 2) return 1;
	asumi = N % K;
	iguti = K - asumi;
	printf("%" PRIu64 "\n", asumi <= iguti ? asumi : iguti);
	return 0;
}
