#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(v) v.begin(),v.end()

const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) {
    return (b==0) ? a : gcd(b, a%b);
}

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// MODなし版
ll choose(ll n){
    return (n)*(n-1)/2;
}

// 以下から
const int MAX_N = 100000;
struct Edge {
    int to;
    int id;
};

const int D = 80 * 160 + 10;
const int D2 = D*2;
typedef bitset<D2> bs;
int a[100][100];
//bs dp[90][90];



const int N = 20;
int h;
int w;
//vector<vector<char>> maze(N, vector<char>(N,'#'));

//const int MAX_WH = 21;
//int d[MAX_WH][MAX_WH];
//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
const int INF = 100100100;
//int bfs(int x, int y) {
//    queue<P> que;
//    rep(i,MAX_WH) {
//        rep(j,MAX_WH) {
//            d[j][i] = INF;
//        }
//    }
//    que.push(P(x,y));
//    d[x][y] = 0;
//
//    while (que.size()) {
//        P p = que.front(); que.pop();
//        for (int i=0; i< 4; i++) {
//            int nx = p.first + dx[i];
//            int ny = p.second + dy[i];
//            if (0 <= nx && nx < w && 0 <= ny && ny < h && maze[nx][ny] != '#' && d[nx][ny] == INF) {
//                que.push(P(nx,ny));
//                d[nx][ny] = d[p.first][p.second] + 1;
//            }
//        }
//    }
//    int mx = 0;
//    rep(i,h) rep(j,w) {
//            if (d[j][i] == INF) continue;
//            mx = max(mx, d[j][i]);
//        }
//    return mx;
//}

bool pairCompare(const P& firstElof, const P& secondElof)
{
    return firstElof.first < secondElof.first;
}
void dfs(){

}
const int MX = 100000;
vector<int> G[MX];

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0) res += m;
    return res;
}


int dp[105][105];
char maze[105][105];
void dfs(int x, int y){
    int nextX;
    int nextY;
    //横方向
    nextX = x+1;
    nextY = y;
    if (nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1) {
        int pre =  dp[nextY][nextX];
        if (maze[nextY][nextX] == '#') {
            dp[nextY][nextX] = min(dp[y][x] + 1, dp[nextY][nextX]);
        } else {
            dp[nextY][nextX] = min(dp[y][x], dp[nextY][nextX]);
        }
        if (pre > dp[nextY][nextX]) {
            dfs(nextX, nextY);
        }
    }

    //縦方向
    nextX = x;
    nextY = y+1;
    if (nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1) {
        int pre =  dp[nextY][nextX];
        if (maze[nextY][nextX] == '#') {
            dp[nextY][nextX] = min(dp[y][x] + 1, dp[nextY][nextX]);
        } else {

            dp[nextY][nextX] = min(dp[y][x], dp[nextY][nextX]);
        }
        if (pre > dp[nextY][nextX]) {
            dfs(nextX, nextY);
        }
    }
}

void bfs(){
    queue<P> que;
    que.push({0,0});

    while (! que.empty()) {
        auto p = que.front();que.pop();
        if (p.first == w-1 && p.second == h-1) break;
        int nextX;
        int nextY;
        //横方向
        nextX = (p.first+1);
        nextY = p.second;
        if (nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1) {
            if (dp[nextY][nextX] == INF && dp[p.second][p.first] + 1 < dp[nextY][nextX]){
                que.push({nextX,nextY});
            }
            if (maze[nextY][nextX] == '#') {
                dp[nextY][nextX] = min(dp[p.second][p.first] + 1, dp[nextY][nextX]);
            } else {

                dp[nextY][nextX] = min(dp[p.second][p.first], dp[nextY][nextX]);
            }

        }

        //縦方向
        nextX = p.first;
        nextY = (p.second+1);
        if (nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1) {
            if (dp[nextY][nextX] == INF && dp[p.second][p.first] + 1 < dp[nextY][nextX]){
                que.push({nextX,nextY});
            }
            if (maze[nextY][nextX] == '#') {
                dp[nextY][nextX] = min(dp[p.second][p.first] + 1, dp[nextY][nextX]);
            } else {

                dp[nextY][nextX] = min(dp[p.second][p.first], dp[nextY][nextX]);
            }
            que.push({nextX,nextY});
        }
    }


}

int main(){
    ll x,k; cin >> x >> k;
    ll c = x % k;
    if (c == 0) {
        cout << 0 <<endl;
        return 0;
    } 
    x = min(abs(c-k),x);
    
    cout << x << endl;

}



Measured memory_bytes: 4224