#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define LONGMAX 1e14
#define dd double
#define MOD 1000000007  
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type)) 
#define nl cout<<endl
#define fill(a,val) memset(a,val,sizeof(a))
#define rt ll Q[100005]={0}
#define mp make_pair
//#define endl "\n"
#define test() ll 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<ll>()) 
#define oa(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" ";nl
#define cn(a,n) for(ll i=0;i<n;i++)cin>>a[i];
#define ov(a,n) for(ll i=n-1;i>=0;i--)cout<<a[i]<<" ";nl
#define maxa(ar,N) *max_element(ar,ar+N)
#define mina(ar,N) *min_element(ar,ar+N)
#define INF 1e15
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define printclock cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n";
ll power(ll x, ll y){ll res=1;x=x%MOD;while(y>0){if(y&1)res = (res*x)%MOD;y = y>>1;x = (x*x)%MOD;}return res%MOD;}  
ll logtwo(ll n){if(n==1)return 0;return logtwo(n/2)+1;}
void ceilsearch(ll ar[],ll low,ll high,ll x,ll &y){if(low>high)return;ll mid=(low+high)/2;if(ar[mid]>=x){y=mid;ceilsearch(ar,low,mid-1,x,y);}else{ceilsearch(ar,mid+1,high,x,y);}}
/*struct Node
{
    ll defense;
    ll midfield;
    ll attack;
    ll index;
    Node()
    {
        defense=-INF;
        midfield=-INF;
        attack=-INF;
        index=0;
    }
};
void constructTreeUtil(Node tree[],Node ar[],ll ss,ll se,ll si)
{
    if(se==ss)
    {
        tree[si]=ar[ss];
        return;
    }
    ll mid=(se+ss)/2;
    constructTreeUtil(tree,ar,ss,mid,2*si+1);
    constructTreeUtil(tree,ar,mid+1,se,2*si+2);
    Node left=tree[2*si+1];
    Node right=tree[2*si+2];
    ll r=0;
    if(right.defense>left.defense)
      r++;
    if(right.midfield>left.midfield)
      r++;
    if(right.attack>left.attack)
      r++;
    if(r>=2)
      tree[si]=right;
    else
      tree[si]=left;
}
Node queryUtil(Node tree[],Node ar[],ll ss,ll se,ll low,ll high,ll si)
{
    if(low>se||high<ss)
    {
        Node nullNode;
        return nullNode;
    }
    if(ss >= low && se <= high)
    {
        return tree[si];
    }
    ll mid=(se+ss)/2;
    Node left=queryUtil(tree,ar,ss,mid,low,high,2*si+1);
    Node right=queryUtil(tree,ar,mid+1,se,low,high,2*si+2);
    Node res;
    ll r=0;
    if(right.defense>left.defense)
        r++;
    if(right.midfield>left.midfield)
        r++;
    if(right.attack>left.attack)
        r++;
    if(r>=2)
        res=right;
    else
        res=left;
    return res;
}
void query(Node tree[],Node ar[],ll low,ll high,ll N)
{
    Node res=queryUtil(tree,ar,0,N-1,low,high,0);
    cout<<res.index<<endl;
    return;
}
void updateUtil(Node tree[],Node ar[],ll pos,Node v,ll si,ll ss,ll se)
{
    if(pos>se||pos<ss)
    {
        return;
    }
    if(se==ss)
    {
        tree[si]=v;
        return;
    }
    ll mid=(ss+se)/2;
    updateUtil(tree,ar,pos,v,2*si+1,ss,mid);
    updateUtil(tree,ar,pos,v,2*si+2,mid+1,se);
    Node left=tree[2*si+1];
    Node right=tree[2*si+2];
    ll r=0;
    if(right.defense>left.defense)
      r++;
    if(right.midfield>left.midfield)
      r++;
    if(right.attack>left.attack)
      r++;
    if(r>=2)
      tree[si]=right;
    else
      tree[si]=left;
}
void update(Node tree[],Node ar[],ll pos,Node val,ll N)
{
    ar[pos]=val;
    //ar[pos].index=pos+1;
    //val.index=pos+1;
    updateUtil(tree,ar,pos,val,0,0,N-1);
}
Node tree[500000];*/
ll B;
void solve()
{
    ll N,M;
    cin>>N>>M;
    cout<<min(N%M,M-N%M)<<endl;
}
signed main()
{
    fastio();   
    ll t=1;
    //cin>>t;
    ll gt=1;
    while(t--) 
    {
        solve();
    }   
}

Measured memory_bytes: 256