// This is my template for fast solving
 
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <memory>
#include <initializer_list>
#include <iomanip>
#include <stack>
#include <bitset>
#include <cstdio>
// #include <complex>
// #include <random>
 
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using ld = long double;

// const ll INF = 1e18;
// const ld INF = 1e20;
const ll MOD = 998244353;
// const ld EPS = 1e-10;
// const ld PI = acos(-1);

template<typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p);
template<typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p);
 template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v);
template<typename T>
istream& operator>>(istream& is, vector<T>& v);
template<typename T>
ostream& operator<<(ostream& os, const set<T>& s);
template<typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& m);

vector<int> calc_z(const string& s);
ll binpow(ll a, ll m, ll mod);

void solve() {
    ll n, k;
    cin >> n >> k;
    if (n > k) {
        n %= k;
    }
    set<ll> was;
    while (was.count(abs(n - k)) == 0) {
        was.insert(abs(n - k));
        n = abs(n - k);
    }
    cout << *was.begin() << "\n";
}   
 
int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    // int t;
    // cin >> t;
    // for (int i = 1; i <= t; ++i) {
    //     cout << "Case #" << i << ": "; // CodeJam style
    //     solve();
    // }
 
    // int q;
    // cin >> q;
    // while (q--) {
    //     solve();
    // }

    solve();
 
    return 0;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
    os << p.first << " " << p.second;
    return os;
}

template<typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
    is >> p.first >>  p.second;
    return is;
}
 
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    bool first = true;
    for (const auto& x : v) {
        if (!first) {
            os << " ";
        }
        first = false;
        os << x;
    }
    return os;
}

template<typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (auto& elem : v) {
        is >> elem;
    }
    return is;
}

template<typename T>
ostream& operator<<(ostream& os, const set<T>& s) {
    bool first = true;
    for (auto x : s) {
        if (!first) {
            os << " ";
        }
        first = false;
        os << x;
    }
    return os;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& m) {
    bool first = true;
    for (auto x : m) {
        if (!first) {
            os << "\n";
        }
        first = false;
        os << x;
    }
    return os;
}

vector<int> calc_z(const string& s) {
    int n = s.length();
    vector<int> z(n, 0);
    int l = 0, r = 0;
    for (int i = 1; i < n; ++i) {
        if (i <= r) {
            z[i] = min(z[i - l], r - i);
        }
        int j = i + z[i];
        while (j < n && s[j] == s[z[i]]) {
            ++z[i];
            ++j;
        }
        if (j >= r) {
            r = j - 1;
            l = i;
        }
    }
    return z;
}

ll binpow(ll a, ll m, ll mod = MOD) {
    ll res = 1;
    while (m) {
        if (m % 2) {
            res = res * a % mod;
            --m;
        } else {
            a = a * a % mod;
            m /= 2;
        }
    }
    return res;
}


Measured memory_bytes: 256