#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define gc() getchar()
//#define scanf scanf_s
typedef unsigned long long ull;
typedef  long long ll;
int in() {   // 非負整数の入力
	int n = 0; int c;
	c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}
int comp(const void* a, const void* b)//用来做比较的函数。
{
	return *(int*)a - *(int*)b;
}
int main()
{
	ull n, k,m,t;
	scanf("%llu %llu", &n, &k);
	if (n == k) { printf("0\n"); return 0; }
	if (n < k) printf("%llu\n", n < abs(n-k) ? n : abs(n - k));
	else printf("%llu\n", n%k < abs(k- n % k) ? n % k : abs(k - n % k));
}