#include <bits/stdc++.h>
using namespace std;
string s;
int n;
long long dp[70][2][70];
bool vis[70][2][70];
long long f(int idx, int tight, int cnt, int k) {
  if (idx == n) {
    if (cnt == k) return 1;
    return 0;
  }
  if (vis[idx][tight][cnt]) return dp[idx][tight][cnt];
  vis[idx][tight][cnt] = true;
  long long ans = 0;
  if (tight == 1) {
    if (s[idx] == '1') {
      ans += f(idx + 1, tight, cnt + 1, k);
      ans += f(idx + 1, 0, cnt, k);
    } else {
      ans += f(idx + 1, tight, cnt, k);
    }
  } else {
    ans += f(idx + 1, 0, cnt, k);
    ans += f(idx + 1, 0, cnt + 1, k);
  }
  dp[idx][tight][cnt] = ans;
  return ans;
}
int main() {
  long long m, k, p, mid, val;
  cin >> m >> k;
  long long left = 1;
  long long right = 1000000000000000000LL;
  while (left <= right) {
    mid = (left + right) / 2;
    p = mid;
    s = "";
    while (p > 0) {
      s.push_back((char)(p % 2 + 48));
      p /= 2;
    }
    reverse(s.begin(), s.end());
    n = (int)(s.size());
    memset(vis, false, sizeof(vis));
    val = -f(0, 1, 0, k);
    p = mid * 2;
    s = "";
    while (p > 0) {
      s.push_back((char)(p % 2 + 48));
      p /= 2;
    }
    reverse(s.begin(), s.end());
    n = (int)(s.size());
    memset(vis, false, sizeof(vis));
    val += f(0, 1, 0, k);
    if (val == m) {
      cout << mid << endl;
      break;
    } else if (val > m)
      right = mid - 1;
    else
      left = mid + 1;
  }
  return 0;
}
