#include <bits/stdc++.h>
using namespace std;
const int maxl = 40 + 2;
long long p[maxl], c[maxl][maxl];
void pre() {
  p[0] = 1ll;
  for (int i = 1; i < maxl; i++) p[i] = p[i - 1] * 2ll;
  for (int i = 0; i < maxl; i++) c[0][i] = 1;
  for (int i = 1; i < maxl; i++)
    for (int j = i; j < maxl; j++) c[i][j] = c[i - 1][j - 1] + c[i][j - 1];
}
int Log(long long k) {
  if (k <= 1) return 0;
  return Log(k / 2) + 1;
}
long long fun(long long n, int bit) {
  if (bit == 0) return 1ll;
  if (n <= 0) return 0;
  long long LOG = Log(n);
  if (bit > LOG + 1) return 0;
  long long ret = c[bit][LOG] + fun(n - p[LOG], bit - 1);
  return ret;
}
int main() {
  pre();
  long long n, t;
  cin >> n >> t;
  int an = t;
  while (an && an % 2 == 0) an /= 2;
  if (an != 1) {
    cout << 0 << endl;
    return 0;
  }
  int LL = Log(t);
  cout << fun(n + 1, LL + 1) - (t == 1);
  return 0;
}
