#include <bits/stdc++.h>
const int MAXN = 1e5 * 5;
const int INF = 1e9 + 7;
using namespace std;
long long C[65][65];
long long m, k;
long long tinh(long long x) {
  long long t = 0, num = 0;
  for (int i = 63 - 1; i + 1; i--)
    if (x & (1LL << i)) {
      if (k >= num) t += C[i][k - num];
      num++;
    }
  return t;
}
int a[100];
long long check(long long n) {
  long long f = n, ans = 0;
  for (int i = 0; i < 64; i++) {
    a[i] = f % 2;
    f = f / 2;
  }
  int x = 0;
  for (int i = 63; i > -1; i--) {
    if (a[i] == 0) continue;
    if (x > k || x + i + 1 < k) break;
    ans += C[i][k - x];
    x++;
  }
  return ans;
}
long long calc(long long x) {
  long long ans = 0;
  long long d1 = 0;
  for (int i = 64; i >= 1; i--) {
    if ((x >> (i - 1)) & 1) {
      if (k - d1 >= 1) ans += (C[i - 1][k - d1]);
      d1++;
    }
  }
  return ans + (__builtin_popcountll(x) >= k);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> m >> k;
  memset(C, 0, sizeof(C));
  for (int i = 1; i <= 64; i++) C[i][1] = i;
  for (int i = 1; i <= 64; i++) C[i][0] = 1;
  for (int i = 2; i <= 64; i++)
    for (int k = 1; k <= 64; k++) C[i][k] = C[i - 1][k - 1] + C[i - 1][k];
  long long d = 1, c = 2234567891234567891, g, fin = -1;
  int dem = 0;
  for (int i = 1; i <= 190470; i++)
    if (__builtin_popcount(i) == k) {
      dem++;
    }
  while (d <= c) {
    g = (d + c) / 2;
    long long tmp = calc(g * 2LL) - calc(g);
    if (tmp >= m) {
      if (tmp == m) fin = g;
      c = g - 1;
    } else
      d = g + 1;
  }
  cout << fin;
  return 0;
}
