#include <bits/stdc++.h>
/*
#include<boost/multiprecision/cpp_int.hpp>
namespace mp=boost::multiprecision;
*/
using namespace std;
#define ll long long int
#define lo mp::cpp_int
#define pb push_back
#define pob pop_back
#define mp make_pair
#define endl "\n"
#define ce(x) cout << x <<"\n"
#define fill(s,v) memset(s,v,sizeof(s))
#define speed  { ios_base::sync_with_stdio(false); cin.tie(0); }
#define deb(x) cout<<#x<<" "<<x<<endl; 
#define INFINITE 1000000009
#define ull unsigned long long int
#define take(a,n) for(ll pp=0;pp<n;pp++) { cin>>a[pp]; }
#define take2(a,n) for(ll p=0;p<n;p++) { yo temp;cin>>temp;a.pb(temp); }
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,a,n) for(ll i=a;i<n;i++)
#define show(a,n) for(ll p=0;p<n;p++) cout<<a[p]<<" ";
#define _ <<" "<<
#define nl cout<<endl;
#define sq(x) ((x)*(x))
#define call(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ff first
#define ss second
#define lld long double
#define sc(n) scanf("%lld",&n);
#define de2(x,y) cout<<#x<<" = "<<(x) <<' '<< #y<<" = "<<y<<endl;typedef pair<int, int> pii;
#define copy(b,a,n) for(ll i=0;i<n;i++) b[i]=a[i];
#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (int _tt = 0; _tt < tt; _tt++) 
typedef vector<vector<ll>> matrix ;  
typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;   
const ll dx[4] = {-1, 0, 1, 0};
const ll dy[4] = {0, -1, 0, 1};
const ll mod = 1000000007;
ll gcd(ll a,ll b){if(!b)return a;return gcd(b,a%b);} 
//ll power_mod(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;} 
ll modexpo(ll x,ll n,ll M){if(n==0)return 1;else if(n%2 == 0)return modexpo((x*x)%M,n/2,M);else return(x*modexpo((x*x)%M,(n-1)/2,M))%M;}
ll lcm(ll a, ll b){return (a*b)/gcd(a, b);}
ll sumofdigits(ll n){ll out=0;while(n){out+=(n%10);n/=10;}return out;}
bool isPrime(ll n){if (n <= 1)  
    return false; if (n <= 3)  return true; if (n%2 == 0 || n%3 == 0) return false; 
    for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;return true;} 
// ll power(ll a,ll b){if(b==0)return 1;if(b&1)return a*power(a,b-1);ll res=power(a,b/2);return res*res;}
ll bexpo(ll x,ll n){if(n==0)return 1;else if(n%2==0)return bexpo(x*x,n/2);else return x*bexpo(x*x,(n-1)/2);}
ll log(ll d){return log2(d);}
bool isPower(ll x, ll y){if(x==1) return (y==1); ll pow=1; while(pow<y) pow*=x; return(pow == y);}  
bool isPerfectSquare(long double x){long double sr = sqrt(x);return ((sr - floor(sr)) == 0);}   
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b) { return (a.second < b.second);} 
ll isPalindrome(string s){ll l=0;ll h=s.size()-1;while(h>l){if(s[l++]!=s[h--])return 0;}return 1;} 
ll setbits(ll n) { if(n==0)return 0; else return 1+setbits(n&(n-1)); } 
ll modInverse(ll A,ll M){return modexpo(A,M-2,M);}
//------modular inverse by extended euclid-------//

// ll d, x, y;
// void extendedEuclid(ll A, ll B) {
//  if(B == 0) {
//  d = A;
//  x = 1;
//  y = 0;
//  }
//  else {
//  extendedEuclid(B, A%B);
//  ll temp = x;
//  x = y;
//  y = temp - (A/B)*y;
//  }
// }

// ll modInverse2(ll A, ll M){
// extendedEuclid(A,M);
// return (x%M+M)%M;
// }

//---------------------------------//

//---------------matrix exponentiation-------------------//

// matrix mul(matrix a,matrix b){
//   matrix res(n+1,vector<ll>(n+1));
//   f(i,n)f(j,n)f(k,n)res[i][j]=(res[i][j]%mod+((a[i][k]%mod)*(b[k][j]%mod))%mod)%mod;
//    return res;
// }
// matrix pow(matrix A,ll p) {
//     if(p==1) return A;
//     matrix res=pow(A,p/2) ;
//     res=mul(res,res);
//     if(p&1) res=mul(res,A) ;
//     return res;
// }

//-------------------------------------------------//

vector<ll> factorize(ll n) {
    vector<ll> res;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            res.push_back(i);
            n /= i;
        }
    }
    if (n != 1) {
        res.push_back(n);
    }
    return res;
}

vector<ll> divisors(ll n){ 
    vector<ll> res;
    for (ll i=1; i<=sqrt(n); i++){ 
        if (n%i == 0){ if (n/i == i){ res.pb(i); } else{ res.pb(n/i); res.pb(i); } } 
    } 
    return res;
}   
vector<ll> primeFactors(ll n){  
    if(n==1) return vector<ll>(1,1);
    bool idx[1]={false};
    vector<ll> res;
    while (n%2 == 0){  
        if(!idx[1]){idx[1]=true;res.pb(2);}
        n = n/2;  
    }  
    for (ll i=3;i*i<=n; i+=2){
        bool once=false;
        while (n%i==0){ if(!once){res.pb(i);once=true;} n=n/i; }  
    }  
    if (n > 2)res.pb(n);
    return res;
}

ll phi(ll n) 
{ 
    float result = n;  
    for (ll p = 2; p * p <= n; ++p) { 
        if (n % p == 0) { 
            while (n % p == 0) 
                n /= p; 
            result *= (1.0 - (1.0 / (float)p)); 
        } 
    } 
    if (n > 1) 
        result *= (1.0 - (1.0 / (float)n)); 
    return (ll)result%mod; 
} 

ll phi2(ll n)
{
    ll m=n;
    double ans=n;
    for(ll i=2;i*i<=n;i++)
    {
        ll count=0;
        while(n>1&&n%i==0)
        {
            n=n/i;
            count++;
        }
        if(count>0)
        {
            ans=ans*(1.0-(1.0/i));
        }
    }
    if(n>1)
    {
            ans=ans*(1.0-(1.0/n));
    }
    ll ans2=ans;
    return ans2%mod;
}

//-------------dfs------------------//

//ll n;
//100005
//vector<ll> ed[100005];
//vector<ll> vec[100005];
//bool check[100005];
//ll com[100005];
//ll nodes;

// void dfs(ll n){
//  nodes++;
//  check[n]=true;
//  for(ll i=0;i<ed[n].size();i++){
//      if(!check[ed[n][i]]){
//          dfs(ed[n][i]);
//      }
//  }
// }

//--------------------------------------------//

ll djikshtra(ll n,vector<pii> graph[],ll src,ll dst)
{
  vector<ll> dist(n+1,LONG_MAX);
  priority_queue<pii,vector<pii>,greater<pii>> heap;
  vector<bool> visited(n+1,false);
  dist[src] = 0;
  heap.push({dist[src],src});
  while(!heap.empty())
  {
    pii curr = heap.top();
    heap.pop();
    if(visited[curr.second])  continue;
    visited[curr.second] = true;
    for(auto it: graph[curr.second])
    {
      if(visited[it.first])  continue;
      if(dist[it.first] > dist[curr.second] + it.second)
      {
        dist[it.first] = dist[curr.second] + it.second;
        heap.push({dist[it.first],it.first});
      }
    }
    if(curr.second == dst)
      break;
  }
  return dist[dst];
}


ll dxx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dyy[] = {0, 1, 1, 1, 0, -1, -1, -1};

//----check if a point is in range-----///

//bool check(ll x,ll y){return(x>=0&&x<n&&y>=0&&y<m);}

//--------------------------------------------////

//---sieve of eranthoses------//

// #define size 1000001
// char prim[1000001];
// ll sum[1000001];

// void gen()
// {
//     for(ll i=2; i*i<size;++i)
//     {
//         if(prim[i]==0) // i is a prime number
//         {    
//             for(ll j=i<<1;j<size; j+=i)
//                 prim[j] = 1; // j is not a prime number
//         }
//     }   
//     sum[0] = sum[1] = 0;
//     for(ll i = 2; i < size; ++i)
//     {
//         sum[i] = prim[i] == 0;
//         if(i)
//             sum[i] += sum[i - 1];
//     }
// }

//------------------------//
const int FULL_MASK = (1 << 10) - 1;


void solve(){
    ll n,k; cin>>n>>k;
    if(n==k){
        ce("0");
    }
    else if(n>k){   
        ll g=n%k;
        cout<<min(min(abs(g-k),g),abs(abs(g-k)-k));
    }
    else{
        ll g=k-n;
        cout<<min(g,n);
    }
}  

int main (){
    speed
    //tc(tt){
        solve();
    //}
}



/*

4
4 3
5 1 2 1
4 10
11 9 11 9
2 5
4 3
3 7
9 4 9

1
4 3
5 1 2 1

*/      

Measured memory_bytes: 256