/* 
    @author : _0-1_nap_nap_
*/

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/assoc_container.hpp> 

using namespace std;
using namespace __gnu_pbds;

//------------------------MACROS START HERE----------------------------------//

#define     PB                      push_back
#define     MP                      make_pair
#define     endl                    '\n'
#define     MAX                     LLONG_MAX
#define     MIN                     LLONG_MIN
#define     rep(i, a, b, inc)       for(long long i = (LL)a; i < (LL)b; i += inc)
#define     REP(i, n)               rep(i, 0, n, 1)
#define     MEM(a, b)               memset(a, (b), sizeof(a))
#define     PI                      3.1415926535897932384626433832795
#define     MOD                     1000000007
#define     GCD(a, b)               __gcd(a, b)
#define     LCM(a, b)               (a * b)/GCD(a, b)
#define     ff                      first
#define     ss                      second
#define     pall(a)                 REP(i, (LL)sizeof(a)/(LL)sizeof(a[0]))cout << a[i] << " ";cout << endl;
#define     pvall(v)                REP(i, (LL)v.size())cout << v[i] << " ";cout << endl;
#define     pset(s)                 cout << "{";for(auto thing : s)cout << thing << ", ";cout << "}" << endl;
#define     pmap(m)                 for(auto thing : m)cout << "[" << thing.ff << ", " << thing.ss << "]" << endl;
#define     pvpair(v)               pmap(v)
#define     gall(a, n)              REP(i, n)cin >> a[i];
#define     all(x)                  x.begin(), x.end()
#define     rall(x)                 x.rbegin(), x.rend()

typedef 	unsigned long long 		ULL;
typedef 	long long 				LL;
typedef 	long double 			LD;
typedef 	pair<LL, LL> 			PLL;
typedef 	vector<LL> 				VL;
typedef 	vector<string> 			VS;
typedef     vector<PLL>             VLL;
typedef     vector<VL>              VVL;
typedef     map<LL, LL>             MPLL;
typedef		unordered_map<LL, LL>	UMPLL;
typedef		set<LL>					SETL;
typedef		unordered_set<LL>		USETL;
typedef		multiset<LL>			MSETL;
typedef     tree <PLL, null_type,less<PLL>, rb_tree_tag,tree_order_statistics_node_update>		ordered_set_PLL;      
typedef     tree <LL, null_type,less<LL>, rb_tree_tag,tree_order_statistics_node_update>		ordered_set;        

//query 1 order_of_key (k)  : Number of items strictly less than k.
//query 2 find_by_order(k)  : k-th element in the set (counting from zero).
//less<ll>                  : query 1 will return numbers strictly less than k
//greater<ll>               : query 1 will return numbers strictly greater than k


//------------------------MACROS END HERE-------------------------------------//

//------------------------tmpl8 FUNCTIONS START HERE--------------------------//

void    fast            ()                                          {ios_base::sync_with_stdio(false);cin.tie(NULL);}
VL      sieve           (LL n)                                      {vector<bool> is_prime(n + 1, true);is_prime[0] = false;is_prime[1] = false;VL primes;for(LL i = 2; i * i <= n; ++i){if(is_prime[i]){for(LL j = 2 * i; j <= n; j += i){is_prime[j] = false;}}}REP(i, is_prime.size()){if(is_prime[i]){primes.PB(i);}}return primes;}
LL      power_mod       (LL x, LL y, LL m)                          {LL res = 1;while(y > 0){if(y & 1){res = (res * x) % m;}y >>= 1;x = (x * x) % m;}return res;}
LL      mod_inverse     (LL x, LL m)                                {return power_mod(x, m - 2, m);}
LD      dist2d          (LL x1, LL y1, LL x2, LL y2)                {return sqrt((LD)((x1 - x2) * (x1 - x2)) + (LD)((y1 - y2) * (y1 - y2)));}
LD      dist3d          (LL x1, LL y1, LL z1, LL x2, LL y2, LL z2)  {return sqrt((LD)((x1 - x2) * (x1 - x2)) + (LD)((y1 - y2) * (y1 - y2)) + (LD)((z1 - z2) * (z1 - z2)));}
LD      slope           (LL x1, LL y1, LL x2, LL y2)                {return (LD)(y2 - y1) / (LD)(x2 - x1);}
LL      dot_product     (VL a, VL b)                                {LL sum = 0;REP(i, a.size()){sum += a[i] * b[i];}return sum;}

//------------------------tmpl8 FUNCTIONS END HERE------------------------------//

//---------------------------------custom---------------------------------------//



//---------------------------------custom---------------------------------------//

//------------------------PROGRAM STARTS HERE----------------------------------//

using namespace std;

int main()
{
    fast();
	LL n, k;
	cin >> n >> k;
	LL x = n / k;
	n -= k * x;
	LL res = n;
	res = min(res, abs(n - k));
	cout << res << endl;
}







Measured memory_bytes: 256