#include <stdio.h>

long abs(long p){
  return(p>=0?p:-p);
}

long min(long p0,long p1){
  return(p0<p1?p0:p1);
}

int main(){
  long n,k,m;
  scanf("%ld",&n);
  scanf("%ld",&k);
  m=abs(n%k);
  printf("%ld",min(abs(k-m),m));
  return(0);
}