#include <bits/stdc++.h>
using namespace std;
vector<int> asdf;
unsigned long long choose[64][64];
unsigned long long solve(int k, int n) {
  if (k == 0) return 1;
  if (n == asdf.size()) return 0;
  if (asdf[n] == 0) return solve(k, n + 1);
  return choose[asdf.size() - 1 - n][k] + solve(k - 1, n + 1);
}
int main() {
  unsigned long long n, t;
  cin >> n >> t;
  if (t != 1 && (t & (t - 1)) != 0) {
    cout << 0 << endl;
    return 0;
  }
  memset(choose, 0, sizeof choose);
  for (int i = 0; i < 64; i++) choose[i][0] = choose[i][i] = 1;
  for (int i = 2; i < 64; i++)
    for (int j = 1; j < i; j++)
      choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j];
  n++;
  int p = 1;
  for (unsigned long long a = 1; a < t; p++, a *= 2)
    ;
  for (; n; n /= 2) asdf.push_back(n & 1);
  reverse(asdf.begin(), asdf.end());
  cout << (solve(p, 0) + (t == 1 ? -1 : 0)) << endl;
  return 0;
}
