#include <bits/stdc++.h>
const long long M = 105;
long long m, k, a[M], dp[M][M];
long long dfs(const long long pos, const long long sum, const bool lim) {
  if (!pos) return sum == k;
  if (!lim && ~dp[pos][sum]) return dp[pos][sum];
  long long up = lim ? a[pos] : 1, res = 0;
  for (long long i = 0; i <= up; ++i)
    if (sum + (i == 1) <= k)
      res += dfs(pos - 1, sum + (i == 1), lim && i == up);
  if (!lim) dp[pos][sum] = res;
  return res;
}
long long solve(long long x) {
  long long len = 0;
  while (x) {
    a[++len] = x & 1;
    x >>= 1;
  }
  return dfs(len, 0, 1);
}
long long work(const long long x) { return solve(x << 1) - solve(x); }
signed main() {
  memset(dp, -1, sizeof dp);
  scanf("%lld%lld", &m, &k);
  long long l = 0, r = 1e18, mid, tmp;
  while (l + 1 < r) {
    mid = l + r >> 1;
    tmp = work(mid);
    if (tmp == m) return !printf("%lld", mid);
    if (tmp < m)
      l = mid;
    else
      r = mid;
  }
  return 0;
}
