#include <stdio.h>
int main(void){
  int N, K, a, b;
    scanf("%d", &K); scanf("%d", &N);
    if (N < 0) N = N * -1;
    a = K % N;
    b = a - N; 
  	if(b < 0) b = b * -1;
    if (a < b) printf("%d", a);
    else printf("%d", b);
    return 0;
}