#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, t;
  cin >> n >> t;
  int x = 1;
  while (t > 1) {
    if (t & 1) {
      cout << 0 << endl;
      return 0;
    }
    t /= 2;
    ++x;
  }
  ++n;
  vector<vector<long long> > dp(x + 1, vector<long long>(2, 0));
  dp[0][0] = 1;
  while (n > 0) {
    vector<vector<long long> > next(x + 1, vector<long long>(2, 0));
    for (int i = 0; i <= x; ++i) {
      if (n & 1) {
        if (i < x) {
          next[i + 1][0] += dp[i][0];
          next[i + 1][1] += dp[i][1];
        }
        next[i][0] += dp[i][0] + dp[i][1];
      } else {
        if (i < x) {
          next[i + 1][1] += dp[i][0] + dp[i][1];
        }
        next[i][0] += dp[i][0];
        next[i][1] += dp[i][1];
      }
    }
    dp.swap(next);
    n /= 2;
  }
  if (x == 1)
    cout << (dp[x][0] - 1) << endl;
  else
    cout << dp[x][0] << endl;
  return 0;
}
