#include <stdio.h>

int rem(int x,int y){
  int r = x%y;
  return r;
}

void printmin(int a, int b){
  if(a < b){
    printf("%d\n",a);
    return;
  }else{
    printf("%d\n",b);
    return;
  }
}

int main(void){
  int N,K;
  scanf("%d",&N);
  scanf("%d",&K);
  printmin(rem(N,K),K-rem(N,K));
  return 0;
}
