#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)
#define rf(i,n) for(int i=n-1;i>=0;i--)
#define PI 3.1415926535897932384626433832795
#define fi first
#define se second
#define mp(x,y) make_pair
#define BIN(n,x) bitset<n>(x)  
using namespace std;

//nC2
ll C2(ll n){
    ll ans= max(((n*(n-1))/2),(ll)0);
    return ans;
}

//GCD
ll gcd(ll a,ll b){
    if(a<b){
        swap(a,b);
    }
    if(a%b==0){
        return b;
    }
    else{
        return gcd(b,a%b);
    }
}

//BITSET
//bitset< ##no of digits required in binary## > x( ##integral string value## )   // bitset< ##no of digits required in binary## > x( ##number## ) 
//stores binary value of the string in x // constructor sets all values to 0 by default // x.set() sets all value to 1 //x.none() checks for 0{gives bool}
//-->x.count() gives no of 1's //--> x.size() gives total bits //--> x.test(i) returns binary value of i'th bit

//SETPRECISION
//cout<<fixed<<setprecision(#n#)<<x;  --> sets the no of digits of x after decimal to n

//SET STL
//insert();*** count() returns 1 if element is present or 0  **** ;find() {returns iterator pointing to element else returns end iterator}; ---> O(log(N))
//begin();end(){next to last};---->O(1)
//See sorting

//VECTOR STL
//begin(); back();-->O(1)
//sort(v.begin(),v.end()); -->{O(Nlog(N))}
//set_union(it1beg,it1end,it2beg,it2end,itcombinedbegin)   {joins two vectors}
//lower_bound(v.begin(),v.end(),val) returns iterator to element just greater than or eq to val--->O(N)
//sort(v.begin(),v.end()) is sorted default by first value of pairs  to sort by second

//MAP STL map<U,V> mp; U=key val  V = mapped val
//count(k) (returns 1 if element with key value k is found}-->O(log(N))
//See sorting



//NO OF DIVISORS of x
        // double x1=sqrt(x);
        // int x2=sqrt(x1);
        // int nDiv=0;
        // if(x1==x2){
        //     f1(i,x2){
        //         if(x%i==0){
        //             nDiv+=2;
        //         }
        //     }
        //     nDiv++;
        // }
        // else{
        //     f1(i,x2+1){
        //         if(x%i==0){
        //             nDiv+=2;
        //         }
        //     }
        // }
//SIEVE
    // int pr[100000+1]={0};
    // pr[0]=-1;
    // pr[1]=-1;
    // int curr;
    // for(int j=2;j<=100000;j++){
    //     if(pr[j]!=-1){
    //         curr=j;
    //         for(int i=curr*2;i<=100000;i+=curr){
    //             pr[i]=-1;
    //         }
    //     }
    // }

//EULER TOTIENT
    // vector<int> phi(1000001);
    // phi[0]=0;
    // phi[1]=1;
    // for(int i=2;i<=1000001;i++){
    //     phi[i]=i;
    // }
    // for(int i=2;i<=1000001;i++){
    //     if(phi[i]==i){
    //         for(int j=i;j<=1000001;j+=i){
    //             phi[j]=phi[j]-phi[j]/i;
    //         }
    //     }
    // }
    // //phi[i]=i-1 ==> no is prime
//Binary Exponentiation
long long BERem(long long a, long long b, long long m){
    a%=m;
    long long res =1;
    while(b>0){
        if(b&1){
            res = (res * a)%m;
        }
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}


int main(){
    
    ll n,k;
    cin>>n>>k;
    ll rem=n%k;
    ll rem1=abs(rem-k);
    cout<<min(rem,rem1);
    


    return 0;
}



Measured memory_bytes: 256