#include<stdio.h>
#include<stdlib.h>
long long MIN(long long a,long long b){return a<b?a:b;}
int main(){
  long long a,b;
  scanf("%lld %lld",&a,&b);
  printf("%lld\n",MIN(a%b,labs(b-a%b)));
  return 0;
}
