#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T b, T p, T M) {
  T ret = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1) ret = (ret * b) % M;
    b = (b * b) % M;
  }
  return (T)ret;
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  return a * b / gcd<T>(a, b);
}
template <class T>
inline T power(T b, T p) {
  long long int x = 1;
  while (p--) x *= b;
  return x;
}
const int mod = 1000000007;
string s;
long long int m, k, DP[65][65][2];
long long int call(long long int pos, long long int need, long long int small) {
  if (need < 0LL) return 0LL;
  if (pos == s.size()) return need == 0LL;
  long long int &ret = DP[pos][need][small];
  if (ret != -1) return ret;
  ret = 0LL;
  for (long long int d = 0; d <= 1; d++) {
    if (small || d <= (s[pos] - '0')) {
      ret += call(pos + 1, need - d, small || d < (s[pos] - '0'));
    }
  }
  return ret;
}
void iniBinary(long long int n) {
  s = "";
  while (n) {
    if (n % 2)
      s += '1';
    else
      s += '0';
    n /= 2;
  }
  reverse(s.begin(), s.end());
}
long long int solve(long long int num) {
  iniBinary(num);
  memset(DP, -1, sizeof(DP));
  long long int A = call(0, k, 0);
  iniBinary(num * 2);
  memset(DP, -1, sizeof(DP));
  long long int B = call(0, k, 0);
  return B - A;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  cin >> m >> k;
  long long int lo = 1, hi = 1000000000000000000;
  while (lo <= hi) {
    long long int mid = (lo + hi) / 2;
    long long int cnt = solve(mid);
    if (cnt == m) {
      cout << mid << endl;
      break;
    }
    if (cnt < m)
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  return 0;
}
