//04/03/2020
#include<bits/stdc++.h>
#include<cmath>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define debug printf("Debug\n");
#define en '\n'


#define ll long long
#define ull unsigned long long

#define pi acos(-1.0)
#define mod 1000000007
#define INF 1e18
#define _INF -1e18

#define _fastio  ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define in_fre  freopen("in.txt", "r", stdin);
#define out_fre freopen("out.txt", "w", stdout);

struct Node{
    ll val,pos;
};
struct OverloadedNode{
    ll val,pos;
    bool operator < (const OverloadedNode &rhs) const {
        if(val==rhs.val) return pos<rhs.pos;
        else return val<rhs.val;
    }
};
//Algos
template<typename T> using Pbds=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
//typedef tree<OverloadedNode,null_type,less<OverloadedNode>,rb_tree_tag,tree_order_statistics_node_update>OverloadedPbds;
vector<ll>graph[200005]; ll vis[200005];
void CLR(ll n){ for(ll i=0;i<=n+1;i++){graph[i].clear();vis[i]=0;}}
void bfs();
void dfs();

//Number Theory
template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T> T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}

//Geometry
struct CartesianPoint{double x,y;};
struct PolarPoint{double r,ang;};

template <typename T> T SQR(T x){return x*x;}
template <typename T> T DOT(T x1,T y1,T x2,T y2){ return x1*x2+y1*y2;}
template <typename T> T CROSS(T x1,T y1,T x2,T y2){ return x1*y2-x2*y1;}
template <typename T> T PolarR(T x,T y){ return sqrt(SQR(x)+SQR(y));}
template <typename T> T PolarAngle(T x,T y){ return atan2(double(y),double(x));}
template <typename T> T CartesianX(T r,T ang){ return r*cos(double(ang));}
template <typename T> T CartesianY(T r,T ang){ return r*sin(double(ang));}
template <typename T> T CartesianDis(T x1,T y1,T x2,T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}
template <typename T> T PolarDis(T r1,T ang1,T r2,T ang2){return sqrt(SQR(r1)+SQR(r2)-2.00*r1*r2*cos(ang1-ang2));}
template <typename T> T MiddlePoint(T x1,T x2){return (x1+x2)/2;}
template <typename T> T InternalSection(T x1,T x2,T m1,T m2){ return (m1*x2+m2*x1)/(m1+m2);}
template <typename T> T ExternalSection(T x1,T x2,T m1,T m2){ return (m1*x2-m2*x1)/(m1-m2);}
template <typename T> T AreaOfTriangle(T x1,T y1,T x2,T y2,T x3,T y3){ return (x1*y2+x2*y3+x3*y1)-(y1*x2+y2*x3+y3*x1);}
template <typename T> T GradAngle(T x1,T y1,T x2,T y2){ return atan2( double(y1-y2) , double(x1-x2));}
template <typename T> T Grad(T x1,T y1,T x2,T y2){ return (x1-x2)==0.00?1.00*1e18:( double(y1-y2) / double(x1-x2));}
template <typename T> T Line(T m,T c,T x,T y){ return y-m*x-c;}
template <typename T> T Line(T m,T x1,T y1,T x,T y){ return (y-y1)-m*(x-x1);}
template <typename T> T Line(T x1,T y1,T x2,T y2,T x,T y){ return (y2-y1)*x-(x2-x1)*y+(x1*y2-y1*x2);}
template <typename T> T OrthogonalLine(T x1,T y1,T x2,T y2,T lx,T ly,T ox,T oy){ return -lx*(x2-x1)-ly*(y2-y1)+(y2-y1)*ox+(x2-x1)*oy;}
template <typename T> T LineOrthogonalLineIntersecX(T x1,T y1,T x2,T y2,T ox,T oy){ return ((x1-x2)*((y2-y1)*ox+(x2-x1)*oy)-(y1-y2)*(x1*y2-x2*y1))/((y2-y1)*(y1-y2)-SQR(x1-x2));}
template <typename T> T LineOrthogonalLineIntersectY(T x1,T y1,T x2,T y2,T ox,T oy){ return ((x1-x2)*(x1*y2-x2*y1)-(y2-y1)*((y2-y1)*ox+(x2-x1)*oy))/((y2-y1)*(y1-y2)-SQR(x1-x2));}
template <typename T> bool IsParallel(T x1,T y1,T x2,T y2){ (CROSS(x1,y1,x2,y2)==0.00)?true:false;}
template <typename T> bool IsOrthogonal(T x1,T y1,T x2,T y2){ (DOT(x1,y1,x2,y2)==0.00)?true:false;}
template <typename T> T IntersecX(T a1,T b1,T c1,T a2,T b2,T c2){ return (b1*c2-b2*c1)/(a1*b2-a2*b1);}
template <typename T> T IntersecY(T a1,T b1,T c1,T a2,T b2,T c2){ return (b1*c2-b2*c1)/(a1*b2-a2*b1);}
template <typename T> T PointToLineDis(T a,T b,T c,T x,T y){ return (a*x+b*y+c)/sqrt(SQR(a)+SQR(b));}
template <typename T> T PointToLineDis(T x1,T y1,T x2,T y2,T x,T y){ return PointToLineDis(y2-y1,x1-x2,x1*y2-x2*y1);}
template <typename T> T LineToLineDis(T a,T b,T c1,T c2){ return abs(c1-c2)/sqrt(SQR(a)+SQR(b));}
template <typename T> ll isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y);}


/*Containers*/
//vector<ll>v;
//deque<ll>dq;
//stack<ll>stk;
//map<ll,ll>mp;
//unordered_map<ll,ll>ump;
//set<ll>st;
//multiset<ll>mst;
//unordered_set<ll>ust;
//unordered_multiset<ll>umst;
//priority_queue<ll,deque<ll>,greater<ll>>pq;
//Pbds<ll>ost;
/*OverLoaded Containers*/
//vector<OverloadedNode>v;
//deque<OverloadedNode>dq;
//stack<OverloadedNode>stk;
//map<OverloadedNode,OverloadedNode>mp;
//set<OverloadedNode>st;
//multiset<OverloadedNode>mst;
//priority_queue<OverloadedNode>pq;
//OverloadedPbds oost;
/*Global Variables*/
map<ll,ll>mp;

int main(){
    _fastio
    ll n,k;
    cin>>n>>k;
    if(n==k){
        cout<<0<<en;
        return 0;
    }
    else if(n>k){
        cout<<min(n%k,k-n%k)<<en;
        return 0;
    }
    else{
        cout<<min(n,k-n)<<en;
    }
    return 0;
}

void bfs(ll src){
    vis[src]=1;
    deque<ll>dq;
    dq.push_back(src);
    while(!dq.empty()){
        src=dq.front();
        dq.pop_front();
        for(ll i=0;i<graph[src].size();i++){
            ll adj=graph[src][i];
            if(vis[adj]==0){
                vis[adj]=1;
                dq.push_back(adj);
            }
        }
    }
}

void dfs(ll src){
    vis[src]=1;
    for(ll i=0;i<graph[src].size();i++){
        ll adj=graph[src][i];
        if(!vis[adj]){
            dfs(adj);
        }
    }
}


Measured memory_bytes: 4992