#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll GCD(ll a, ll b){ return a ? GCD(b % a, a) : b; }
int main(){
    ll n,k;
    cin>>n>>k;
    ll b=n%k;
    cout<<min(b,abs(b-k))<<endl;
}