#include <stdio.h>
#include <math.h>

typedef unsigned long long int ULLI;

int main()
{
	ULLI n, k;

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

	if (k != 1) {
		while (abs(n - k) < n) {
			n = abs(n - k);
		}
	}
	else
		n = 0;

	printf("%llu\n", n);
}