#include <stdio.h>
#define MIN(x,y) (((x) > (y)) ? (y) : (x))
#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
int main() {
  long long n, k;
  scanf("%lld %lld", &n, &k);
  long long rem = n % k;
  printf("%lld", MIN(rem, ABS(rem - k)));
}