#include <stdio.h>
#include <stdlib.h>
int main(){
    long long n,k,x,y;
    scanf("%llu %llu", &n,&k );
    x = n%k; y = abs(x-k);
    if (x>y)
    printf("%llu\n",y);
    else
    printf("%llu\n",x );
}
