#include <stdio.h>
int min(int a, int b){
  if (a <= b)
    return a;
  else
    return b;
}

int main(){
  int n, k;
  int x;
  x = scanf("%d%d", &n, &k);
  int a, b;
  a = (n % k);
  b = (k - a);
  printf("%d\n", min(a, b));
}