#include<stdio.h>

long int abs_(long int x){
	if(x < 0){
		x = -x;
	}
	return x;
}

int main(){
	long int N, K;
	scanf("%ld %ld", &N, &K);
	//printf("%d\n", abs_(K));
	if(N%K == 0){
		printf("%d\n", 0);
	}else{
		N = N%K;
		while(abs_(N-K)%K < N%K) N = abs_(N-K)%K; 
		printf("%ld\n", N);
	}
	return 0;
}