#include <bits/stdc++.h>
using namespace std;
long long n, t, ans;
long long a[50];
long long f[50][50];
int e;
long long getit(long long r, int e) {
  if (e == -1)
    if (r >= 1) return 1;
  if (r <= 4) {
    int tq = 0, t0 = 0, t1 = 0;
    if (r >= 1) ++tq;
    if (r >= 2) ++t0;
    if (r >= 3) ++t0;
    if (r >= 4) ++t1;
    if (e == -1)
      return tq;
    else if (e == 0)
      return t0;
    else if (e == 1)
      return t1;
    else
      return 0;
  }
  int t;
  for (int i = 1; i <= 49; i++)
    if (a[i] >= r) {
      t = i - 1;
      break;
    }
  return f[t][e] + getit(r - a[t], e - 1);
}
int main() {
  a[0] = 1;
  for (int i = 1; i <= 49; i++) a[i] = (long long)a[i - 1] * 2;
  memset(f, 0, sizeof(f));
  f[2][0] = 2;
  f[2][1] = 1;
  f[3][0] = 3;
  f[3][1] = 3;
  f[3][2] = 1;
  for (int i = 4; i <= 49; i++) {
    for (int j = 0; j <= 49; j++) f[i][j] = f[i - 1][j] + f[i - 1][j - 1];
    ++f[i][0];
  }
  cin >> n >> t;
  e = 0;
  while (t % 2 == 0) {
    e++;
    t /= 2;
  }
  if (t != 1) {
    cout << 0 << endl;
    return 0;
  }
  ans = getit(n + 2, e);
  if (e == 0) --ans;
  cout << ans << endl;
  return 0;
}
