#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const double PI = 3.1415926535897932384626433832795;
const double EPS = 1e-12;
const int inf = 1000000000;
const long long linf = 1000000000000000000LL;
template <class T>
inline T sqr(T x) {
  return x * x;
}
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long dp[100][100];
long long doit(long long n, int k) {
  if (!n) return 0;
  if (!k) return 1;
  int i = 0;
  while ((1LL << (i + 1)) <= n) ++i;
  long long r = dp[i][k];
  r += doit(n - (1LL << i), k - 1);
  return r;
}
int main() {
  dp[0][0] = 1;
  for (int i = 1; i < 100; ++i) {
    for (int j = 0; j < 100; ++j) {
      dp[i][j] = dp[i - 1][j];
      if (j) dp[i][j] += dp[i - 1][j - 1];
    }
  }
  long long n, t;
  cin >> n >> t;
  ++n;
  int k = 0;
  while (!(t & (1LL << k))) ++k;
  if (t != (1LL << k)) {
    cout << 0;
    return 0;
  }
  long long res = 0;
  for (int i = 0; n; ++i) {
    if ((1LL << i) <= n) {
      n -= 1LL << i;
      res += dp[i][k];
    } else {
      res += doit(n, k);
      n = 0;
    }
  }
  if (t == 1) --res;
  cout << res;
  return 0;
}
