#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef vector<LL> vl;
typedef pair<LL, LL> pl;
typedef vector<pair<LL, LL>> VP;
typedef tuple<LL, LL, LL> tl;
const LL N=pow(10, 9)+7;
#define ull unsigned long long int 
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define RW freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#define endl "\n"
#define REP(i, a, b) for(LL i=a; i<b; i++)
#define RREP(i, a, b) for(LL i=a; i>b; i--)
#define WE(i) while(i--)
#define PB push_back
#define PF pop_front
#define MP make_pair
#define MT make_tuple
#define F first
#define S second

LL sod(LL a){ // returns sum of digits of a decimal number
    LL sum=0;
    while(a!=0)
    {
        sum=sum+a%10;
        a=a/10;
    }
    return sum;
}

LL num1(LL a){ // returns the count of number of 1's in binary representation of a decimal number
    LL s=0;
    while(a!=0){
        if(a%2==1){
            s++;
        }
        a=a/2;
    }
    return s;
}

LL bmod(string s, LL a){ // returns remainder when a binary number in string s is divided by a decimal number a
    LL res=0;
    REP(i, 0, s.length()){
        res=(10*res+(LL)s[i]-'0')%a;
    }
    return res;
}

LL pd(LL n) { // returns the number of divisors of n
    LL ans=0, x=sqrt(n);
    if(x%1==0){
        REP(i, 1, x+1){
            if(n%i==0){
                ans+=2;
            }
            if(n==i*i){
                ans--;
            }
        }
    }
    else{
        REP(i, 0, x){
            if(n%i==0){
                ans+=2;
            }
        }
    }
    return ans;
}

vl primes(LL n)  {  // returns all the prime factors of a number n in a vector
    // Print the number of 2s that divide n  
    vl v;
    while (n % 2 == 0)  
    {  
        v.PB(2); 
        n = n/2;  
    }  
  
    // n must be odd at this point. So we can skip  
    // one element (Note i = i +2)  
    for (int i = 3; i <= sqrt(n); i = i + 2)  
    {  
        // While i divides n, print i and divide n  
        while (n % i == 0)  
        {  
            v.PB(i);
            n = n/i;  
        }  
    }  
  
    // This condition is to handle the case when n  
    // is a prime number greater than 2  
    if (n > 2)  
        v.PB(n);  
    return v;
}  

LL fact(LL n) { // returns factorial of a number n
    if (n == 0) 
    return 1; 
    return n * fact(n - 1); 
}

double dist(LL x1, LL y1, LL x2, LL y2){ // returns the euler distance between two points in a 2D plane
    LL dx=x1-x2, dy=y1-y2;
    return(pow(dx*dx+dy*dy, 0.5));
}

LL MIN(LL a, LL b){ // returns the minimum of two numbers a and b
    if(a<b){
        return a;
    }
    else{
        return b;
    }
}

LL MAX(LL a, LL b){ // returns the maximum of two numbers a and b
    if(a>b){
        return a;
    }
    else{
        return b;
    }
}

void swap(LL *a, LL *b){ // swaps the two numbers
    LL *x=a;
    a=b;
    b=x;
}

vl SieveOfEratosthenes(LL n){ // returns a vector containing all the primes upto the number n 
    vl v;
    // Create a boolean array "prime[0..n]" and initialize 
    // all entries it as true. A value in prime[i] will 
    // finally be false if i is Not a prime, else true. 
    bool prime[n+1]; 
    memset(prime, true, sizeof(prime)); 
  
    for (LL p=2; p*p<=n; p++) 
    { 
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
            // Update all multiples of p greater than or  
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked.  
            for (LL i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    // Print all prime numbers 
    for (LL p=2; p<=n; p++) 
       if (prime[p]) 
            v.PB(p);
    return v; 
}

void solve(){
    // LL n, m;
    // LL sum=0;
    // cin>>n>>m;
    // LL a[n];
    // REP(i, 0, n){
    //     cin>>a[i];
    //     sum+=a[i];
    // }

    // sort(a, a+n);
    // reverse(a, a+n);
    // LL x=0;
    // LL y=sum/(4*m);
    // REP(i, 0, n){
    //     if(a[i]>y) x++;
    // }
    // if(x>=m) cout<<"Yes"<<endl;
    // else cout<<"No"<<endl;

    LL n, k;
    cin>>n>>k;
    vl v;
    v.PB(n%k);
    while(v[v.size()-1]-k>0){
        v.PB(v[v.size()-1]-k);
    }
    v.PB(abs(v[v.size()-1]-k));
    sort(v.begin(), v.end());
    cout<<v[0]<<endl;
}

int main(){
    // FAST // untie cin and cout
    // RW // I/O in input.txt and output.txt
    LL T=1;
    //cin >> T;
    WE(T){
        solve();
    }
    return 0;
}

Measured memory_bytes: 256