#include <stdio.h>
typedef long long ll;

int main(){
	ll n, k, a, dummy;
	scanf("%lld %lld",&n,&k);
	a = n%k;
	dummy = a-k;
	if(dummy<0) dummy*=-1;
	if(dummy<a) printf("%lld",dummy);
	else printf("%lld",a);
}
