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

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