#include<stdio.h>

int main(){
	long long n, k;
	long long amari, min;

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

	amari = n % k;
	min = (amari < k - amari) ? amari : k - amari;

	printf("%lld", min);
	return 0;
}
