#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
#define loop(i, l, r) for(i=l; i<r; i++)/*Instead of function, (for).*/
#define rep(i,r) for(i=0; i<r; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long int lli;
/*Return Combination ,(nCr)*/
int combi(int n, int r)
{
	if(r==0 || r==n) return 1;
	else return combi(n-1,r) + combi(n-1,r-1);
}
/*Return gcd(a, b)*/
int gcd(int a, int b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}
//qsort�֐��Ɏg��compare�֐�,(long long int, int, double�^)
int compare_lli(const void *a, const void *b)
{
    return *(lli*)a - *(lli*)b;
}
int compare_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}
int compare_double(const void *a, const void *b)
{
    return *(double*)a - *(double*)b;
}
/*qsort(data, 10, sizeof(int_lli_double), compare_int);*/
/*qsort(�z��, �v�f��, sizeof(�z��̌^), compare_�z��̌^);*/
int chek(int *tmp1, int *tmp2, int n){
	int i;
	for(i=0; i<n; i++){
		if(tmp1[i] != tmp2[i]){
			return 1;
		}
	}
	return 0;
}//2�̔z�񂪈�v���邩�̔���_(return��v��0,�s��v���P)
lli Abs(lli a){
	if(a>=0){
		return a;
	}
	else{
		return -1*a;
	}
}
int main(void)
{
	lli n, k, ans, m, old;
	scanf("%lld%lld", &n, &k);
	if(n>k){
		m = n/k;
		n -= k*m;
		if(n == 0) ans = 0;
	}
	old = n;
	while(1){
		n = Abs((n-k));
		if(n > old){
			ans = old;
			break;
		}
		if(n == 0){
			ans = 0;
			break;
		}
		old = n;
	}
	printf("%lld", ans);
	return 0;
}