#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define dd double
#define MOD 1000000007
#define nl cout<<endl
#define rt int Q[100005]={0}
#define mp make_pair
#define test() int t;cin>>t;while(t--)
#define pb push_back
#define ff first    
#define ss second
#define all(v) v.begin(),v.end()
#define s(ar,n) sort(ar,ar+n)
#define rs(ar,n) sort(ar, ar+n, greater<int>()) 
#define oa(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" ";nl
#define cn(a,n) for(int i=0;i<n;i++)cin>>a[i];
#define ov(a,n) for(int i=n-1;i>=0;i--)cout<<a[i]<<" ";nl
#define ovp(a,n) for(int i=0;i<n;i++)cout<<a[i].ff<<" "<<a[i].ss<<endl;
#define maxa(ar,N) *max_element(ar,ar+N)
#define mina(ar,N) *min_element(ar,ar+N)
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
typedef pair<int,int> pii;
ll powe(ll n,ll m){if(m==0)return 1;ll t=powe(n,m/2);if(m%2==0)return (t*t);return (((t*t))*n);}
ll mpowe(ll n,ll m){if(m==0)return 1;ll t=mpowe(n,m/2);t%=MOD;if(m%2==0)return (t*t)%MOD;return (((t*t)%MOD)*n)%MOD;}
ll logtwo(ll n){if(n==0)return 0;return logtwo(n/2)+1;}
ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a;
    return gcd(b, a % b);  
      
}
/*void build(ll node,ll start,ll end)
{
    if(start==end)
    {
        tree[node]=a[start];
        
    }
    else
    {
        ll mid = (start+end)/2;
        build(2*node+1,start,mid);
        build(2*node+2,mid+1,end);
        tree[node]=gcd(tree[2*node+1],tree[2*node+2]);
    }
}
void update(ll tree[],ll node,ll start,ll end,ll idx)
{
    if(start==end)
    {
        tree[node] = max(0LL,tree[node]-1);
    }
    else
    {
        ll mid = (start+end)/2;
        if(start<=idx && mid>=idx)
        {
            update(tree,2*node+1,start,mid,idx);
        }
        else
        {
            update(tree,2*node+2,mid+1,end,idx);
        }
        tree[node] = tree[2*node+1]+tree[2*node+2];
    }
}
ll query(ll node,ll start,ll end,ll l,ll r)
{
    if(r<start || l>end)
    {
        return 0;
    }
    if(l<=start && r>=end)
    {
        return tree[node];
    }
    ll mid = (start+end)/2;
    ll p1 = query(2*node+1,start,mid,l,r);
    ll p2 = query(2*node+2,mid+1,end,l,r);
   return gcd(p1,p2);
}
*/
bool bp(std::vector<int> v[],int color[],int x,int visited[])
{
    color[x]=0;
    queue <int> q;
    q.push(x);
    while(!q.empty())
    {
        int s = q.front();
        visited[s]=1;
        q.pop();
        for(int i=0;i<v[s].size();i++)
        {
            if(color[v[s][i]]==-1)
            {
                color[v[s][i]]=1-color[s];
                q.push(v[s][i]);
            }
            else if(color[v[s][i]]==color[s])
            {
                return false;
            }
        }
    }
    return true;

}
bool isbp(std::vector<int> v[],int n,int color[])
{
    int visited[n]={0};
    for(int i=0;i<n;i++)
        color[i]=-1;
    for(int i=0;i<n;i++)
    {
        if(color[i]==-1)
        {
            if(!(bp(v,color,i,visited)))
            {
                return false;
            }
        }
    }

    return true;
}

int main()
{                       
    fastio();
    ll a,b;
    cin>>a>>b;
    ll c = min(a%b,(b-a%b)%b);
    cout<<c<<endl;
}

Measured memory_bytes: 256