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

int main (void)
{
  unsigned long N, K;
  unsigned long div, a, b;
  
  scanf("%lu %lu", &N, &K);
  
  if (N < K) {
    printf("%lu", N);
    return 0;
  } else {
    div = N / K;
    a = abs(N - (K * div));
    b = abs(N - (K * (div * 1)));
    
    printf("%lu", a>b?a:b);
    
    return 0;
  }
    
}
