#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 40;
long long N, T, ans, p[45], num[45][45];
int t, lim;
bool power_of_two(long long x) {
  double p = log(x) / log(2.0);
  return (fabs(p - floor(p)) < 1e-7);
}
void init() {
  p[0] = 1LL;
  for (int i = (int)1; i <= (int)MAX_N + 3; i++) p[i] = p[i - 1] * 2LL;
  memset(num, 0LL, sizeof num);
  num[0][0] = 1LL;
  num[1][0] = 2LL;
  num[1][1] = 1LL;
  for (int i = (int)2; i <= (int)MAX_N; i++)
    for (int j = (int)0; j <= (int)MAX_N; j++) {
      num[i][j] = num[i - 1][j] + num[i - 1][j] - num[i - 2][j];
      if (j > 0) num[i][j] += num[i - 1][j - 1] - num[i - 2][j - 1];
    }
}
long long find(long long len, int idx, int target) {
  if (len == 0) return 0LL;
  if (idx == 0) return ((target == 0) ? 1LL : 0LL);
  if (target == 0) return 1LL;
  if (len < p[idx - 1]) return find(len, idx - 1, target);
  return num[idx - 1][target] - ((idx >= 2) ? num[idx - 2][target] : 0LL) +
         ((target > 0) ? find(len - p[idx - 1], idx - 1, target - 1) : 0LL);
}
int main() {
  init();
  cin >> N >> T;
  t = -1;
  for (int i = (int)0; i <= (int)MAX_N; i++)
    if (p[i] == T) {
      t = i;
      break;
    }
  if (t == -1) {
    puts("0");
    return 0;
  }
  lim = 0;
  while (p[lim + 1] - 2LL <= N) lim++;
  ans = num[lim - 1][t];
  N -= (p[lim] - 2LL);
  ans += find(N, lim, t);
  if (t == 0) ans--;
  cout << ans << endl;
}
