#include <stdio.h>

int main(void){

  long n,k;
  scanf("%ld %ld", &n, &k);

	long answer;

	long diff_a ,diff_b;

	if (n > k) {
		diff_a = n-k;
		diff_a = diff_a%k;
		diff_b = k-diff_a;
		answer = diff_a;
		if (diff_b < diff_a) {
			answer = diff_b;
		}
	}
	if (n == k) {
		answer = 0;
	}
	if (n < k) {
		diff_a = n;
		diff_b = k-diff_a;
		answer = diff_a;
		if (diff_b < diff_a) {
			answer = diff_b;
		}
	}

	printf("%ld\n", answer);

  return 0;
}