#include <stdio.h>

int main(int argc, char const *argv[]) {

  unsigned long long int N, K;
  scanf("%lld %lld", &N, &K);

  unsigned long long int A, B;
  A = N % K;

  if (A == 0) {
    printf("0\n");
  }else{
    B = (A - K) * (-1);
    if (A < B) {
      printf("%lld\n", A);
    }else{
      printf("%lld\n", B);
    }
  }

  return 0;
}
