#include <bits/stdc++.h>
using namespace std;
long long dp[105][105];
long long m;
int k;
int num[105];
long long DFS(int pos, int sum1, bool limit) {
  if (pos == -1) {
    if (sum1 == k)
      return 1;
    else
      return 0;
  }
  if (limit == 0 && dp[pos][sum1] != -1) return dp[pos][sum1];
  long long sum = 0;
  int up = limit ? num[pos] : 1;
  for (int i = 0; i <= up; i++) {
    int tp = sum1;
    if (i == 1) tp++;
    sum += DFS(pos - 1, tp, limit && i == num[pos]);
  }
  if (limit == 0) dp[pos][sum1] = sum;
  return sum;
}
long long Solve(long long n) {
  int kk = 0;
  while (n) {
    num[kk++] = n % 2;
    n /= 2;
  }
  return DFS(kk - 1, 0, 1);
}
long long bin() {
  long long l = 1, r = 1e18;
  while (l < r) {
    long long mid = (l + r) / 2;
    long long tans = Solve(2 * mid) - Solve(mid);
    if (tans == m)
      return mid;
    else if (tans > m)
      r = mid - 1;
    else
      l = mid + 1;
  }
}
int main() {
  while (~scanf("%lld%d", &m, &k)) {
    memset(dp, -1, sizeof(dp));
    long long ans = bin();
    printf("%lld\n", ans);
  }
}
