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

#define MOD 1000000007
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define ll long long
#define inf 100000000000000000
int addMod(int x, int y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

int mulMod(int x, int y)
{
    return (x * 1ll * y) % MOD;
}

int binpow(int x, int y)
{
    int z = 1;
    while(y)
    {
        if(y & 1) z = mulMod(z, x);
        x = mulMod(x, x);
        y >>= 1;
    }
    return z;
}

int inv(int x)
{
    return binpow(x, MOD - 2);
}

int divide(int x, int y)
{
    return mulMod(x, inv(y));
}

ll gcd(int a, int b) 
{   if (a == 0) return b; 
    if (b == 0) return a;  
    if (a == b) return a; 
    if (a > b)  return gcd(a-b, b); 
    return gcd(a, b-a); 
} 

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

ll h(ll* A, int N){
    ll pro = 1;
    for(int i=0; i<N; ++i) pro = mulMod(A[i], pro);
    return pro;
}

ll f(ll* A, int N){
    int alpha = A[0];
    for(int i=1; i<N; ++i){
        alpha = gcd(alpha, A[i])%MOD;
    }
    return alpha;
}

void generateNormal(string ans, int N, int charUsed){
    if(N==0) {cout<<ans<<endl; return;}
    for(int i=0; i<charUsed+1; ++i){
        char currentChar = 'a' + i;
        generateNormal(ans+currentChar, N-1, i+1);
    }
    return;
}
int N, X;
int A[100005];
vector <int> adj[100005];
int dfs(int u, int p){
    int sum = A[u];
    for(auto &i:adj[u]){
        if(i!=p){
            sum += dfs(i, u);
        }
    }
    return max(sum, -X);
}

void cl()
{
    for(int i=1;i<=N;i++)
        adj[i].clear();
}

unsigned long long int counting(int a, int b){
    unsigned long long int count = 1;
    while(a>0 || b>0){
        if(a&1 && b&1){
            count = mulMod(2, count);
        }
        a = a>>1;
        b = b>>1;
    }
    return count;
}


int solve(priority_queue<int> A, 
          priority_queue<int> B,
          priority_queue<int> C, int X, int Y)
{
    cout<<"INside"<<endl;
    if(X==0 && Y==0) return 0;
    int alpha = A.top();
    int beta = B.top();
    int gamma = C.top();
    A.pop();
    int alpha1 = 0, beta1 = 0, gamma1 = 0, gamma2 = 0;
    if(X!=0)
    alpha1 = alpha + solve(A, B, C, X-1, Y);
    A.push(alpha);
    B.pop();
    if(Y!=0)
    beta1 = beta + solve(A, B, C, X, Y-1);
    B.push(beta);
    C.pop();
    if(X!=0)
    gamma1 = gamma + solve(A, B, C, X-1, Y);
    if(Y!=0)
    gamma2 = gamma + solve(A, B, C, X, Y-1);
    C.push(gamma);
    return max(alpha1, max(beta1, max(gamma1, gamma2)));
}

int zero_one_knapsack(int* val, int* weights, int n, int W)
{
    int dp_table[n+1][W+1];
    for(int i=0; i<n+1; i++)    // This is n
    {
        for(int j=0; j<W+1; j++)    // This is w
        {
            if(i==0 || j==0) dp_table[i][j] = 0;
            else if(weights[i-1]>j) dp_table[i][j] = dp_table[i-1][j];
            else dp_table[i][j] = max(val[i-1] + dp_table[i-1][j-weights[i-1]], dp_table[i-1][j]);
            cout<<dp_table[i][j]<<" ";
        }
        cout<<endl;
    }
    return dp_table[n][W];
}

class node{
    public:
    int data;
    node* left = NULL;
    node* right = NULL;
};

int isSumProperty(node* Node){
    int left_data = 0, right_data = 0;
    if(Node==NULL || (Node->left==NULL && Node->right==NULL)) return 1;
    if(Node->left!=NULL) left_data = Node->left->data;
    if(Node->right!=NULL) right_data = Node->right->data;
    if((Node->data == left_data + right_data) && (isSumProperty(Node->left) && isSumProperty(Node->right))) return 1;
    else return 0;
}

int largestSumSubarray(int* array, int length)
{
    int dp_table[length] = {0};
    dp_table[0] = array[0];
    int answer = dp_table[0];
    for(int i=1; i<length; ++i){
        dp_table[i] = max(dp_table[i-1] + array[i], array[i]);
        if(dp_table[i]>answer) answer = dp_table[i];
    }
    if(answer<0) return 0;
    return answer;
}   


int main(){
    FIO
    unsigned ll N, K;
    cin>>N>>K;
    if(N>K)
        N = N%K;
    unsigned ll alpha = N;
    unsigned ll beta = K-N;
    cout<<min(alpha, beta)<<endl;
}


Measured memory_bytes: 2560