/*
 
Vijay Krishan Pandey
Final Year, MNNIT Allahabad
#onelasttime
 
*/
 
 
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
 
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
 
using namespace std;
 
const int MAX_N = int(1e6) + 10;
const long long int MOD=1e9+7;
const long long int INF=1e18;
 
typedef long long int ll;
typedef unsigned long long int llu;
 
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
 
 
/*
struct z_function{
  string &txt;
  int *z;
  z_function(string &_txt):txt(_txt){
    z=new int[1+txt.length()];
  }
  void cal(){
    z[0]=0;
    int l=0;
    int r=0;
    for(int i=1;i<txt.length();i++){
    //  cout<<l<<" "<<r<<endl;
      if(i>r){
        l=i;
        r=i;
        while(r<txt.length() && txt[r]==txt[r-l]){
          r++;
        }
      //  cout<<r<<" ";
        z[i]=r-l;
        r=r-1;
      }else{
              if(z[i-l]<r-i+1)
                z[i]=z[i-l];
              else{
                l=i;
                while(r<txt.length() && txt[r]==txt[r-l]){
                  r++;
                }
                z[i]=r-l;
                r=r-1;
              }
      }
    }
  }
};
*/
inline ll mul(ll a,ll b,ll MOD=0){
    MOD=1e9+7;
    a=(a+MOD)%MOD;
    b=(b+MOD)%MOD;
    ll c=a*b;
    c=(c+MOD)%MOD;
    return c;
}
inline ll add(ll a,ll b,ll MOD=0){
    MOD=1e9+7;
    a=(a+MOD)%MOD;
    b=(b+MOD)%MOD;
    ll ans=(a+b);
    ans=(ans+MOD)%MOD;
    return ans;
}
ll power(ll a,ll b,ll c=0){
    ll ans=1;
    while(b){
        if(b&1){
            ans=mul(ans,a,c);
        }
        a=mul(a,a,c);
        b=b/2;
    }
    return ans;
}
template<class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &P){
    in >> P.first >> P.second;
    return in;
}
 
template<class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &P){
    out << "(" << P.first << ", " << P.second << ")";
    return out;
}
template<class T>
istream &operator>>(istream &in, vector<T> &arr){
    for(auto &x: arr) in >> x;
    return in;
}
template<class T>
ostream &operator<<(ostream &out, const vector<T> &arr){
    for(auto &x: arr) out << x << ' '; cout << "\n";
    return out;
}
template<class T>
istream &operator>>(istream &in, deque<T> &arr){
    for(auto &x: arr) in >> x;
    return in;
}
template<class T>
ostream &operator<<(ostream &out, const deque<T> &arr){
    for(auto &x: arr) out << x << ' '; cout << "\n";
    return out;
}
mt19937 rnd(time(0));
 
inline ll gcd(ll a,ll b){
    while(b)a%=b,swap(a,b);
    return a;
}
 
 
struct SOS{
    ll *dp;
    int limit;
    vector<int>&arr;
 
    SOS(int _limit,vector<int>&v):arr(v){
 
        limit=_limit;
        dp=new ll[(1<<limit)+10];
    }
 
    void init(){
        for(int i=0;i<(1<<limit);i++){
            dp[i]=0;
        }
        for(int i=0;i<arr.size();i++){
            dp[arr[i]]++;
        }
    }
 
    void build1(){
        for(int i=0;i<limit;i++){
            for(int j=0;j<(1<<limit);j++){
                if(j&(1<<i)){
                    dp[j]=add(dp[j],dp[j^(1<<i)],MOD);
                }
            }
        }
    }
    void build2(){
        for(int i=0;i<limit;i++){
            for(int j=(1<<limit)-1;j>=0;j--){
                if(j&(1<<i)){
                    dp[j^(1<<i)]=add(dp[j^(1<<i)],dp[j],MOD);
                }
            }
        }
    }
// }
 
};
 
// string s;
int find(int arr[],int x)
{
    while(x!=arr[x])
    {
        x=arr[x];
    }
 
    return x;
}
 
void connect(int x,int arr[],int size[],int comp[],int y)
{
    int rootA=find(arr,x);
    int rootB=find(arr,y);
    if(rootA==rootB)return;
 
    if(size[rootA]<size[rootB])
    {
        arr[rootA]=rootB;
        size[rootB]+=size[rootA];
        comp[rootB]=__gcd(comp[rootA],comp[rootB]);
    }else{
        arr[rootB]=rootA;
        size[rootA]+=size[rootB];
        comp[rootA]=__gcd(comp[rootA],comp[rootB]);
    }
 
 
}
 
#define all(x) (x).begin(),(x).end()
 
void combine(int x,int y,int arr[],int size[])
{
    int rootA=find(arr,x);
    int rootB=find(arr,y);
    if(rootA==rootB)return;
 
    if(size[rootA]<size[rootB])
    {
        arr[rootA]=rootB;
        size[rootB]+=size[rootA];
    }else{
        arr[rootB]=rootA;
        size[rootA]+=size[rootB];
    }
 
 
}
 
/* ordered_set A;
   A.insert(x);
   A.erase(x);
   A.order_of_key(x) // no of elements less than (<) x
   *A.find_by_order(k-1) // kth smallest element
*/
// int n;
// ll dp[15][2][2];
// string s;
 
 
// ll fun(int indx,int tight,int started){
 
//     if(indx==n){
//         return 1;
//     }
 
//     if(dp[indx][tight][started]!=-1){
//         return dp[indx][tight][started];
//     }
    
//     int mx=9;
//     if(tight==0){
//         mx=s[indx]-'0';
//     }
 
//     int c=1;
//     int startt=0;
//     if(started==0){
//         startt=1;
//         c=2;
//     }
 
 
 
//     ll ans=0;
 
//     for(int i=startt;i<=mx;i+=c){
//         ans+=fun(indx+1,(tight|(i<(s[indx]-'0'))),(started|(i>0)));
//     }
 
//     return dp[indx][tight][started]=ans;
 
 
 
    
// }
 
// ll fun22(ll x){
 
//     s=to_string(x);
//     memset(dp,-1,sizeof(dp));
//     n=s.length();
 
//     return fun(0,0,0);
// }


 
int main(){
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);




   ll x,k;

   cin>>x>>k;

   if(x>=k){
    ll diff=(x-k);
    diff=diff/k;
    x-=diff*k;
    x-=k;
   }

   if(x<k){
    x=min(x,(k-x));
   }

   cout<<x<<endl;

    
    
 
 
    
    
    
    
 
    
}


Measured memory_bytes: 256