#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vll> vvll;

#define all(x) x.begin(), x.end()

const int MOD = 1000000009;
const int INF = 2e9;

//TO_STRING
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(char ch){
	string s;
	s += ch;
	return s;
}

string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + 
  	to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + 
  	to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

//DEBUG 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL //compile with -DLOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

const int maxn = 2000000;
vi primes;
bool composite[maxn + 1];

void generatePrimes(){
	for(int i = 2; i < maxn; i++){
		if(!composite[i]){
			primes.push_back(i);
		}
		for(int j = 0; j < (int)primes.size() && i * primes[j] < maxn; j++){
			composite[i * primes[j]] = true;
			if(i % primes[j] == 0){
				break;
			}
		}	
	}
}

#define forn(i, s, n) for (int i = s; i <= n; i++)

int get_int(string &s, map<char, int> &pos, vi &v) {
	int res = 0;
	for (int i = 0; i < (int)s.length(); i++) {
		res = res * 10 + v[pos[s[i]]];
	}
	return res;
}

ll expmod(ll x, ll n, ll mod){
	//x ^ n % mod
	if(n == 0) return 1;
	else if(n % 2 == 0){
		ll temp = expmod(x, n / 2, mod);
		return (temp * temp) % mod;
	}
	else{
		return (expmod(x, n - 1, mod) * x) % mod;
	}
}

ifstream fin;

void dfs(int ni, int nj, int n, vvi &compo, vvi &grid, vi &composize, 
	int pi, int pj, auto &groups) {
	vpi dxy = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
	for (auto it : dxy) {
		int x = ni + it.first, y = nj + it.second;
		if (1 <= x && x <= n && 1 <= y && y <= n && 
			grid[x][y] == grid[ni][nj] && compo[x][y] == 0) {
			compo[x][y] = compo[ni][nj];
			composize[compo[x][y]]++;
			// debug(groups.size());
			groups[{pi, pj}].push_back({x, y});
			// debug(groups.size());
			dfs(x, y, n, compo, grid, composize, pi, pj, groups);
		}
	}
}

void display(int n) {
	n--;
	string x = to_string(n);
	int len = x.length();
	if (len == 3) { cout << x; }
	else if (len == 2) { cout << "0" << x; }
	else if (len == 1) { cout << "00" << x; }
}
void solve() {
	ll n, k;
	cin >> n >> k;
	ll x = n;
	ll ans = x;
	x %= k;
	ans = min(ans, x);
	ans = min(ans, k - x);
		//now x < k, k - x, x, k - x
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);	
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
}

Measured memory_bytes: 256