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

void main(){
  long long int n, k, x, xmin;
  scanf("%lld %lld", &n, &k);
  
  while(x>0)
    x -= k;
  x = abs(x);
  xmin = abs(x-k);
  if(xmin>x)
    xmin = x;
  
  printf("%lld",x);
}