#include <stdio.h>
#include <math.h>
int main() {
    long long n;
    int k,x;
    scanf("%d %d",&n,&k);
    x=n%k;
    if(x==0)
        printf("%d",x);
    else if(x>(k-x))
        printf("%d",abs(k-x));
    else
        printf("%d",x);
    return 0;
}