#include <bits/stdc++.h>
using namespace std;
int main() {
  long long dp[50][50], Q[50];
  long long n, t, current;
  long long answer = 0;
  long long l, c, dZ = 1;
  dp[1][1] = 1;
  for (int i = 2; i <= 40; i++)
    for (int j = 1; j <= i; j++) dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
  Q[1] = 1;
  for (int i = 2; i <= 40; i++) Q[i] = Q[i - 1] * 2;
  cin >> n >> t;
  if (t == 0) {
    cout << 0 << endl;
    return 0;
  }
  current = t;
  l = n;
  while (current > 1) {
    if (current & 1) {
      cout << 0 << endl;
      return 0;
    }
    current /= 2;
    ++dZ;
  }
  c = dZ;
  for (int g = 2; g <= 40 && l; g++) {
    dZ = c;
    if (l >= Q[g])
      n = Q[g];
    else
      n = l;
    l -= n;
    for (int i = 40; i; i--) {
      if (n >= Q[i]) {
        answer += dp[i][dZ];
        n -= Q[i];
        dZ--;
      }
    }
  }
  cout << answer << endl;
  return 0;
}
