#include <bits/stdc++.h>
long long N;
int K, num[50], L, cnt[16];
std::map<std::vector<int>, long long> S[50];
long long F(int x, std::vector<int> y) {
  if (x == 0) return 1;
  auto it = S[x].find(y);
  if (it != S[x].end()) return it->second;
  long long O = 0;
  for (int i = 0; i < K && i < int(y.size()); i++) {
    std::vector<int> nxt = y;
    if (i == int(nxt.size()) - 1) nxt.push_back(0);
    nxt[i]--;
    nxt[i + 1]++;
    O += (nxt[i] + 1) * F(x - 1, nxt);
  }
  return S[x][y] = O;
}
long long check(long long x) {
  L = 0;
  for (; x; x >>= 4) num[L++] = x & 15;
  long long O = 0;
  for (int i = 1; i < L; i++) O += 15 * F(i - 1, {15, 1});
  for (int i = 0; i < 16; i++) cnt[i] = 0;
  for (int i = L - 1; ~i; i--) {
    for (int j = i == L - 1; j < num[i]; j++) {
      cnt[j]++;
      int MAX = 0;
      for (int d = 0; d < 16; d++) MAX = std::max(MAX, cnt[d]);
      if (MAX <= K) {
        std::vector<int> G(MAX + 1, 0);
        for (int d = 0; d < 16; d++) G[cnt[d]]++;
        O += F(i, G);
      }
      cnt[j]--;
    }
    cnt[num[i]]++;
  }
  for (int d = 0; d < 16; d++)
    if (cnt[d] > K) return O;
  return O + 1;
}
int main() {
  scanf("%lld%d", &N, &K);
  long long l = 1, r = 0x6a07d23f8;
  while (l < r) {
    long long m = l + r >> 1;
    if (check(m) >= N)
      r = m;
    else
      l = m + 1;
  }
  printf("%llx\n", l);
  return 0;
}
