#include <bits/stdc++.h>
using namespace std;
long long n, t, c, top, ans, dp[220][220];
int F(long long t) {
  int ret = 0;
  while (!(t & 1ll)) t >>= 1ll, ++ret;
  return (t == 1) ? (ret) : (-1);
}
int main() {
  dp[0][0] = 1ll;
  for (int i = 1; i <= 60; ++i) {
    dp[i][0] = 1;
    for (int j = 1; j <= 60; ++j) dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
  }
  cin >> n >> t;
  t = F(t);
  if (t == -1) {
    puts("0");
    return 0;
  }
  for (c = 2, top = 1; n >= c; n -= c, c <<= 1ll, ++top) ans += dp[top][t];
  for (int i = top - 1; i >= 0; --i) {
    c = 1ll << (long long)i;
    if (n >= c) {
      n -= c;
      ans += dp[i][t--];
      if (t < 0) break;
    }
  }
  cout << ans << endl;
  return 0;
}
