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

long long K = 0;

long long f(long long x) { return llabs(x - K); }

int main()
{
  char in[1000];
  fgets(in, 1000, stdin);
  
  long long x = atoll(strtok(in," "));
  K = atoll(strtok(NULL, " "));
  
  if(x == 0) { printf("%lld", K); return 0; }
  if(x == K) { printf("0"); return 0; }
  
  x %= K;
  long long old = x;
  long long next = f(x);
  for(; old > next && old != next; next = f(next)) { /*printf("%ld %ld\n", old, next);*/ old = next; }
  
  //printf("%ld\n", sizeof(long long));
  printf("%lld", old);
  return 0;
}