#include <stdio.h>
#include <string.h>

int main(void){
    long long n,k,ans,m;
    scanf("%lld%lld",&n,&k);
    ans = n;
    if (n/k > 100000){
        m = n/k;
        n -= m*k;
    }
    for (long long i=0;i<=1001000;i++){
        n -= k;
        if (n < 0){
            n *= -1;
        }
        if (ans > n){
            ans = n;
        }
    }
    printf("%lld",ans);
}