#include<stdio.h>

int zetai(int num);

int main() {
    
    long n,k,ans,a;
    scanf("%ld %ld",&n,&k);
    if(n > k) {
        a = n % k;
    } else {
        a = n;
    }
    if(a < k - a) {
        ans = a;
    } else {
        ans = k-a;
    }

    printf("%ld\n",ans);

    return 0;
}

int zetai(int num) {
    if(num < 0) {
        num *= -1;
    }
    return num;
}