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

void main(){
  long long int n, k, x, xmin;
  scanf("%lld %lld", &n, &k);
  
  if(n<k){
    printf("%lld", (n < llabs(n-k)) ? n : llabs(n-k) );
  }
  else{
    x = n%k;
    printf("%lld", (x < llabs(x-k)) ? x : llabs(x-k) );
  }
}