#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#define ll long long int
int main()
{
    ll N,K;
    int max =0;
    int div =0;
    scanf("%lld %lld",&N,&K);
   if (N==K)printf("0");
   if (N>K)
    {
        if (N%K==0)
        {
            printf("0");
            return 0;
        }
        else
        {
            printf("%lld",N/K);
            return 0;
        }
    }

    else {
    ll min;
    ll a,b,res;
    b= N;
    a= abs(N-K);
    min = a;
        for (ll i = 0;i<N;i++)
        {
            if(a<min)min=a;
           a = abs(a-K);
        }
     printf("%lld",min);
    }


    return 0;
}
