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

int main(){
  unsigned long long n,k,r,q,t;
  unsigned long long min;

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

  q = 1;

  if(k != 0){
    r = n/k;
    q = n % k;
  }

  if(q == 0 && k != 0) printf("0\n");

  else if(q == 1 && k == 0) printf("%ulld", n);

  else{
    min = abs(n - r*k);
    t = abs(min - k);

    if(min > t) printf("%ulld\n", t);

    else printf("%ulld\n", min);
  }

  return 0;
}
