#include"stdio.h"
int main(){
	int n,k,s;
	scanf("%d%d",&n,&k);
	if(n%k==0){printf("0");}
	else if(n%k==1){printf("1");}
	else if(n%k==2){printf("2");}
	else{
		s=n%k;
		printf("%d\n",k%s);
	}
return 0;
}