#include <bits/stdc++.h>
using namespace std;
long long ans, C[120][120], n, t, m, i, o, s, j;
int main() {
  scanf("%I64d%I64d", &n, &t);
  for (s = 1, m = 1; s < t; s <<= 1, m++)
    ;
  if (s != t) return puts("0"), 0;
  if (m == 1) ans--;
  n++;
  for (i = 0; i <= 60; ++i)
    for (j = 0; j <= i; ++j) C[i][j] = !j ? 1 : C[i - 1][j - 1] + C[i - 1][j];
  for (i = 60; i >= 0; --i) {
    if (m < 0) break;
    if (o + (1ll << i) <= n) {
      ans += C[i][m];
      m--;
      o += 1ll << i;
    }
  }
  if (!m) ans++;
  printf("%I64d\n", ans);
}
