/**
 *    author:  otera    
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60;
#define rep(i, n) for(int i = 0; i < n; ++ i)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
    ll x, y; cin >> x >> y;
    if(x >= y) {
        cout << x - y << "\n";
        return;
    }
    ll ans = y - x;
    for(int i = 0; ; ++ i) {
        ll x_ = x * (1LL<<i);
        ll len = abs(x_ - y);
        ll cnt = i;
        // for(int bit = (1LL<<i); bit >= 1; bit >>= 1) {
        //     ll cand1 = len / bit, cand2 = cand1 + 1;
        //     // if(abs(len - bit * cand1) <= abs(len - bit * cand2)) {
        //         cnt += cand1;
        //         len = abs(len - bit * cand1);
        //     // } else {
        //     //     cnt += cand2;
        //     //     len = abs(len - bit * cand2);
        //     // }
        // }
        ll cand1 = len / (1LL<<i), cand2 = cand1 + 1;
        ll tmp1 = cand1 + cnt, len1 = abs(len - cand1 * (1LL<<i));
        vector<bool> check(i, 0);
        for(int j = i - 1; j >= 0; -- j) {
            if(len1 & (1LL<<j)) {
                check[j] = 1;
            } else {
                check[j] = 0;
            }
        }
        int now = 0;
        for(int j = i - 1; j >= 0; -- j) {
            if(check[j] == 1) {
                ++ now;
            }
            if(j == 0 or check[j] == 0) {
                tmp1 += min(2LL, now);
                now = 0;
            }
        }
        ll tmp2 = cand2 + cnt, len2 = abs(len - cand2 * (1LL<<i));
        for(int j = i - 1; j >= 0; -- j) {
            if(len2 & (1LL<<j)) {
                check[j] = 1;
            } else {
                check[j] = 0;
            }
        }
        now = 0;
        for(int j = i - 1; j >= 0; -- j) {
            if(check[j] == 1) {
                ++ now;
            }
            if(j == 0 or check[j] == 0) {
                tmp2 += min(2LL, now);
                now = 0;
            }
        }
        chmin(ans, tmp1);
        chmin(ans, tmp2);
        if(x_ > y) break;
    }
    cout << ans << "\n";
    // int k = 0;
    // while(x * (1LL<<(k + 1)) <= y) ++ k;

}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(20);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

ll g1(ll n) {
    string s = to_string(n);
    sort(RALL(s));
    return stoll(s);
}

ll g2(ll n){
    string s = to_string(n);
    sort(ALL(s));
    return stoll(s);
}

ll f(ll n) {
    return g1(n) - g2(n);
}

int main() {
    ll N; int K;
    cin >> N >> K;

    ll now = N;
    REP(i, K) now = f(now);

    cout << now << endl;
}