#include <bits/stdc++.h>
using namespace std;
long long l, r, m, q, len, a[100], dp[100][100];
long long dfs(long long mq, long long sum, bool t) {
  if (!mq) return (sum == q ? 1 : 0);
  if (!t && dp[mq][sum] != -1) return dp[mq][sum];
  long long k, i, res = 0;
  k = t ? a[mq] : 1;
  for (i = 0; i <= k; i++)
    if (sum + (i == 1) <= q) res += dfs(mq - 1, sum + (i == 1), t && (i == k));
  if (!t) dp[mq][sum] = res;
  return res;
}
long long solve(long long x) {
  len = 0;
  while (x) {
    a[++len] = x & 1;
    x >>= 1;
  }
  return dfs(len, 0, 1);
}
long long work(long long x) { return solve(x * 2) - solve(x); }
int main() {
  long long ty, mid;
  bool flag = true;
  cin >> m >> q;
  memset(dp, -1, sizeof(dp));
  l = 0;
  r = 1e18;
  while (l < r - 1) {
    mid = (l + r) / 2;
    ty = work(mid);
    if (ty == m) {
      cout << mid << endl;
      flag = false;
      break;
    }
    if (ty > m)
      r = mid;
    else
      l = mid;
  }
  if (flag) cout << r << endl;
  return 0;
}
