/***************************************************************************\
                                                                            !
                           JUBAYER ABDULLAH JOY                             !
                  5th Batch, CSE, University of Barishal                    !
            Codeforces = Joy     !!     CodeChef = jubayer_joy              !
            Mail = joy.cse5.bu@gmail.com !! jubayerjoy98@gmail.com          !
                                                                            !
/***************************************************************************/

#pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
#include<bits/stdc++.h>
using namespace std;

typedef     long long               ll;
typedef     unsigned long long      ull;
typedef     long double             ld;
typedef     vector<int>             vi;
typedef     vector<ll>              vl;
typedef     pair<ll,ll>             pl;
typedef     pair<int, int>          pi;

#define     io                  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define     ff                  first
#define     ss                  second
#define     pb                  push_back
#define     pf                  push_front
#define     mp                  make_pair
#define     gcd(a,b)            __gcd(a,b)
#define     lcm(a,b)            (a) * ((b) / gcd(a,b))
#define     f(i,n)              for(ll i=0; i<(n); i++)
#define     all(v)              v.begin(), v.end()
#define     Sort(v)             sort(all(v))
#define     rev(v)              reverse(all(v))
#define     bitcount            __builtin_popcountll
#define     YES                 printf("YES\n")
#define     NO                  printf("NO\n")
#define     spc                 " "
#define     el                  "\n"
#define     die                 return 0

/*** Variadic Functions ***/
#define     _T          template<typename T, typename... Types>
#define     _Tos        template <typename T> ostream &operator
#define     _Tfs        template < typename F, typename S > ostream &operator
#define     D(args...)  do { cerr<<"--DEBUG--\nLine no : "<<__LINE__ <<el<<#args<<" : ";dbg(args);} while(0)

auto mini(auto x) {return x;}   _T auto mini(T a, Types... b){auto x = (a) < mini(b...)?a:mini(b...); return x; }
auto maxi(auto x) {return x;}   _T auto maxi(T a, Types... b){auto x = (a) > maxi(b...)?a:maxi(b...); return x; }
auto sum() {return 0;}          _T auto sum(T a, Types... b) {return (a) + sum(b...); }
auto product() {return 1;}      _T auto product(T a, Types... b) {return (a) * product(b...); }
void in() {return;}             _T void in(T &a, Types&... b) { cin>>(a); in(b...);}
void out() {return;}            _T void out(T a, Types... b) { cout<<(a); out(b...);}
void outln(){cout<<el; return;} _T void outln(T a, Types... b) { cout<<(a); outln(b...);}
void dbg () { cerr << el; }     _T void dbg( T a, Types... b) { cerr<<(a)<<' '; dbg(b...); }

_Tos << ( ostream &os, vector<T> &v)    { for(auto it : v) os<<(it)<<spc; return os << "\n"; }
_Tos << ( ostream &os, set<T> &v)       { for(auto it : v) os<<(it)<<spc; return os << "\n"; }
_Tfs << ( ostream &os, pair<F,S> &p ) { return os<<"\n("<<(p.first)<<", "<<(p.second) << ")";}
_Tfs << ( ostream &os, map<F,S> &v )  { for(auto it : v) os<<el<<(it.first)<<" = "<<(it.second);return os<<el;}

/*** Movement ***/
#define valid(new_x,new_y,row,column)  (new_x)>=0 && (new_x)<(row) && (new_y)>=0 && (new_y)<(column)
//vi fx={+1,-1,+0,+0};             // Four Direction
//vi fy={+0,+0,+1,-1};             // Four Direction
//vi fx={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move - 8 Direction
//vi fy={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move - 8 Direction
//vi fx={-2,-2,-1,-1,+1,+1,+2,+2}; // Knights Move
//vi fy={-1,+1,-2,+2,-2,+2,-1,+1}; // Knights Move

//vector<vi> matrix(row, vi (column,value)); //- 2D row column initialized
//vector<vi> list(row);  //- 2D List
//cout << fixed << setprecision(20)<< value <<endl;
//sort(all(a), greater <int>());


int main(int argc, char const *argv[])
{
    ll n, m;
    in(n,m);
    if(n>m)
    {
        if(n%m==0)
            cout<<0;
        else
        {
            while(n>m)
            {
                n =(n - ((ll)n/m) *m);
            }
            cout<<min(n, abs(m-n));

        }
    }
    else
    {
        cout<<min(n, abs(n-m));
    }


    outln();



    return 0;
}













Measured memory_bytes: 256