#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long dp[66][66][2], vis[66][66][2];
int vid;
long long go(int i, int rem, bool isLess, int digit[], int n) {
  if (rem < 0) return 0;
  if (i == n) return rem == 0;
  long long &ret = dp[i][rem][isLess];
  if (vis[i][rem][isLess] == vid) return ret;
  vis[i][rem][isLess] = vid;
  ret = 0;
  if (isLess == true) {
    ret += go(i + 1, rem - 1, true, digit, n);
    ret += go(i + 1, rem, true, digit, n);
  } else {
    if (digit[i] == 1) ret += go(i + 1, rem - 1, false, digit, n);
    ret += go(i + 1, rem, (digit[i] == 1), digit, n);
  }
  return ret;
}
int lenEn, lenSt, st[66], en[66];
void process(long long num) {
  memset(en, 0, sizeof(en));
  memset(st, 0, sizeof(st));
  lenEn = 0, lenSt = 0;
  long long num2 = (2LL * num);
  while (num2) {
    en[lenEn] = (num2 % 2);
    num2 /= 2;
    lenEn++;
  }
  while (num) {
    st[lenSt] = (num % 2);
    num /= 2;
    lenSt++;
  }
  reverse(st, st + lenSt);
  reverse(en, en + lenEn);
}
int main() {
  fast();
  long long m;
  int k;
  cin >> m >> k;
  long long low = 1, high = 1e18, mid, ans = -1;
  while (low <= high) {
    mid = low + (high - low) / 2;
    vid++;
    process(mid);
    long long f = go(0, k, false, st, lenSt);
    vid++;
    long long s = go(0, k, false, en, lenEn);
    if (s - f >= m)
      high = mid - 1, ans = mid;
    else
      low = mid + 1;
  }
  cout << ans << endl;
}
