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

#define rep(i,l,n) for(int (i)=(l);(i)<(n);(i)++)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define abs(a) (a)>0?(a):-(a)
#define ll long long

int main(){
	ll N,K; scanf("%lld %lld",&N,&K);
	N %= K;
	printf("%lld",min(N,abs(N-K)));
	return 0;
}
