#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret) {
  char c;
  int sgn;
  T bit = 0.1;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  if (c == ' ' || c == '\n') {
    ret *= sgn;
    return 1;
  }
  while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10;
  ret *= sgn;
  return 1;
}
using namespace std;
long long c[55][55];
void init() {
  for (int i = int(0); i <= int(50); i++) c[i][0] = 1;
  for (int i = int(1); i <= int(50); i++)
    for (int j = int(1); j <= int(50); j++) {
      c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
    }
}
long long getnum(long long pos, long long len, int n, int cot) {
  if (cot < 0 || cot > n) return 0;
  if (len == 1) return 1;
  if (pos > len / 2)
    return c[n - 1][cot] + getnum(pos - len / 2, len / 2, n - 1, cot - 1);
  else
    return getnum(pos, len / 2, n - 1, cot);
}
int main() {
  init();
  long long m, t;
  scanff(m);
  scanff(t);
  long long x = 1;
  int cot = 0;
  while (x < t) {
    x <<= 1;
    cot++;
  }
  if (x != t) {
    printf("0\n");
    return 0;
  }
  long long ans = 0;
  x = 2;
  long long y = 0;
  int n = 0;
  while (y < m) {
    y += x;
    x <<= 1;
    n++;
  }
  for (int i = int(max(cot, 1)); i <= int(n - 1); i++) ans += c[i][cot];
  ans += getnum(m - (y - x / 2), x / 2, n, cot);
  printf("%lld\n", ans);
  return 0;
}
