#include <bits/stdc++.h>
using namespace std;
int main() {
  long long dp[60][60], a[60];
  long long n, t, cur;
  long long ans = 0;
  long long l, c, d = 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];
  a[1] = 1;
  for (int i = 2; i <= 40; i++) a[i] = a[i - 1] * 2;
  cin >> n >> t;
  if (t == 0) {
    cout << 0 << endl;
    return 0;
  }
  cur = t;
  l = n;
  while (cur > 1) {
    if (cur & 1) {
      cout << 0 << endl;
      return 0;
    }
    cur = cur / 2;
    ++d;
  }
  c = d;
  for (int g = 2; g <= 40 && l; g++) {
    d = c;
    if (l >= a[g])
      n = a[g];
    else
      n = l;
    l = l - n;
    for (int i = 40; i; i--) {
      if (n >= a[i]) {
        ans = ans + dp[i][d];
        n = n - a[i];
        d--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
