#include <bits/stdc++.h>
using namespace std;
long long k;
int n, m, x, y, z, w, t, cnt_state;
int num[750000][16], transit[750000][16], tmp_cnt[16];
long long dp[10][750000][2], ori_state[750000], fac[20], fac11[20], high, low,
    mid, tmp;
int A[20], calc[20], zero_state;
vector<pair<long long, int> > M[1000009];
vector<int> v[10];
void DFS(int x, int sum, long long state) {
  if (t > 1 && sum > 8) return;
  if (t == 1 && sum > 9) return;
  if (x == 16) {
    cnt_state++;
    if (state == 0ll) zero_state = cnt_state;
    M[state % 1000007ll].push_back(make_pair(state, cnt_state));
    for (int i = (0); i < (16); i++) num[cnt_state][i] = tmp_cnt[i];
    ori_state[cnt_state] = state;
    return;
  }
  for (int i = (0); i <= (t); i++) {
    tmp_cnt[x] = i;
    DFS(x + 1, sum + i, state * 11 + i);
  }
}
void to_hex(long long mid) {
  long long val = mid;
  for (int i = (8); i >= (0); i--) {
    A[i] = (int)(val / fac[i]);
    val %= fac[i];
  }
}
int find_state(int state, int add) { return transit[state][add]; }
long long check(long long mid) {
  to_hex(mid);
  calc[9] = 1;
  for (int i = (8); i >= (0); i--) {
    calc[i] = calc[i + 1];
    if (A[i] > 0) calc[i] = 0;
  }
  for (int i = (0); i <= (9); i++) v[i].clear();
  memset(dp, 0ll, sizeof(dp));
  dp[9][zero_state][1] = 1ll;
  v[9].push_back(zero_state);
  int j;
  for (int i = (8); i >= (0); i--) {
    dp[i][zero_state][calc[i]] = 1ll;
    v[i].push_back(zero_state);
    for (int J = (0); J < (v[i + 1].size()); J++) {
      j = v[i + 1][J];
      for (int add = (0); add < (16); add++) {
        if (j == zero_state && add == 0) continue;
        x = find_state(j, add);
        if (x == -1) continue;
        if (dp[i][x][0] == 0ll && dp[i][x][1] == 0ll) v[i].push_back(x);
        dp[i][x][0] += dp[i + 1][j][0];
        if (add <= A[i]) {
          if (add == A[i])
            dp[i][x][1] += dp[i + 1][j][1];
          else
            dp[i][x][0] += dp[i + 1][j][1];
        }
        if (dp[i][x][0] == 0ll && dp[i][x][1] == 0ll) v[i].pop_back();
      }
    }
  }
  long long ans = 0;
  for (int i = (1); i <= (cnt_state); i++) ans += dp[0][i][0] + dp[0][i][1];
  return ans;
}
long long ans, K;
int freq[20];
int valid(int n) {
  to_hex((long long)n);
  int st = 0;
  for (int i = (0); i < (16); i++) freq[i] = 0;
  for (int i = (8); i >= (0); i--) {
    if (A[i] > 0) st = 1;
    if (st == 0) continue;
    freq[A[i]]++;
  }
  for (int i = (0); i < (16); i++)
    if (freq[i] > t) return 0;
  return 1;
}
int main() {
  scanf("%lld %d", &k, &t);
  fac[0] = fac11[0] = 1ll;
  for (int i = (1); i <= (8); i++) fac[i] = fac[i - 1] * 16;
  for (int i = (1); i <= (16); i++) fac11[i] = fac11[i - 1] * 11;
  DFS(0, 0, 0);
  for (int state = (1); state <= (cnt_state); state++)
    for (int add = (0); add < (16); add++) {
      if (num[state][add] + 1 > t) {
        transit[state][add] = -1;
        continue;
      }
      tmp = ori_state[state] + fac11[15 - add];
      transit[state][add] = -1;
      x = (int)(tmp % 1000007ll);
      for (int i = (0); i < (M[x].size()); i++)
        if (M[x][i].first == tmp) transit[state][add] = M[x][i].second;
    }
  high = 4581298449ll * 15ll;
  if (t > 1) high = 2387826724ll;
  low = 0ll;
  if (t <= 2) {
    while (high - low > 1) {
      mid = (high + low) / 2ll;
      if (check(mid) > k)
        high = mid;
      else
        low = mid;
    }
    printf("%llx\n", high);
  } else {
    K = k;
    if (t == 3 && k >= 1400000000)
      k += 17700000;
    else if (t == 3 && k >= 1000000000)
      k += 10000000;
    else if (t == 3 && k >= 500000000)
      k += 5000000;
    ans = check(k) - 1;
    if (ans == K) {
      printf("%llx\n", k);
      return 0;
    }
    for (int i = ((int)k + 1); i <= ((int)k + 30000000); i++) {
      ans += valid(i);
      if (ans == K) {
        printf("%x\n", i);
        return 0;
      }
    }
  }
  return 0;
}
