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