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

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

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

  r = n/k;
  q = n % k;

  if(q == 0) printf("0\n");

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

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

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

  return 0;
}
