#include <bits/stdc++.h>
using namespace std;
long long n, po;
long long c[50][50];
long long ans;
inline void init() {
  ans = 0;
  memset(c, 0, sizeof(c));
  c[0][0] = 1;
  for (register long long i = 1; i < 50; ++i) {
    c[i][0] = 1;
    for (register long long j = 1; j < 50; ++j)
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
  }
  return;
}
inline long long dfs(long long pos, long long len, long long cnt,
                     long long cot) {
  if (cot < 0 || cot > cnt) return 0;
  if (len == 1) return 1;
  long long mid = len / 2;
  if (pos > mid)
    return c[cnt - 1][cot] + dfs(pos - mid, mid, cnt - 1, cot - 1);
  else
    return dfs(pos, mid, cnt - 1, cot);
}
int main() {
  init();
  scanf("%I64d%I64d", &n, &po);
  long long x = 1, cot = 0;
  while (x < po) {
    x <<= 1;
    cot++;
  }
  if (x != po) {
    printf("0\n");
    return 0;
  }
  x = 2;
  long long y = 0, cnt = 0;
  while (y < n) {
    y += x;
    x <<= 1;
    cnt++;
  }
  long long xx = cot > 1 ? cot : 1;
  for (register long long i = xx; i < cnt; ++i) ans += c[i][cot];
  ans += dfs(n - (y - x / 2), x / 2, cnt, cot);
  printf("%I64d\n", ans);
  return 0;
}
