#include<stdio.h>
 
int main(void)
{
  int N, K;
  int kouho1, kouho2;
  int answer;
  
  scanf("%d %d", &N, &K);
  
  kouho1 = N % K;
  kouho2 = K - (N % K);
  if(kouho1 > kouho2) {
    answer = kouho2;
  }else {
    answer = kouho1;
  }
  printf("%d", answer);
  
  return 0;
}