#include <bits/stdc++.h>
long long a[60][60];
int main() {
  int i, j, pp;
  long long n, t, p, ans = 0;
  a[0][0] = 1;
  for (i = 1; i < 50; i++) {
    for (j = 0; j < 50; j++) {
      a[i][j] += a[i - 1][j];
      a[i][j + 1] += a[i - 1][j];
    }
    a[i][1]--;
    a[i][0]++;
  }
  scanf("%I64d%I64d", &n, &t);
  if (t == 1) ans--;
  n++;
  int x = 0;
  p = 1;
  pp = 0;
  while (t > 1) {
    if (t & 1) {
      printf("0\n");
      return 0;
    }
    t >>= 1;
    x++;
  }
  pp = 0;
  while ((1LL << (pp + 1)) <= n) pp++;
  p = 1LL << pp;
  n -= p;
  ans += a[pp][x];
  x--;
  p >>= 1;
  pp--;
  while (x >= 0 && n > 0) {
    if (n >= p) {
      ans += a[pp][x];
      x--;
      n -= p;
    }
    p >>= 1;
    pp--;
  }
  printf("%I64d\n", ans);
  return 0;
}
