#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
const int maxm = 1e6 + 50;
const double eps = 1e-10;
const int max_index = 62;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
inline int read() {
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  int x = 0;
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
long long sum[100];
long long c[100][100];
long long n;
long long get(int i, int x) {
  long long res = 0;
  for (int j = 0; j < min(i, x + 1); j++) {
    res += c[i - j - 1][x - j];
  }
  return res;
}
int cal(long long x) {
  int res = __builtin_popcountll(x) - 1;
  while ((x & 1) == 0) {
    res++;
    x >>= 1;
  }
  return res;
}
long long solve(long long t) {
  if (__builtin_popcountll(t) != 1) return 0;
  long long res = 0;
  if (t == 1) res = -1;
  int x = -1;
  while (t) {
    x++;
    t >>= 1;
  }
  for (int i = 0; (1LL << i) <= n; i++) {
    res += get(i, x);
    if (i == x) res++;
    n -= (1LL << i);
  }
  if (n == 0) return res;
  for (int i = 60; ~i; i--) {
    if (n >> i & 1) {
      res += get(i, x);
      if (i == x) res++;
      x--;
    }
  }
  return res;
}
int main() {
  sum[1] = 1;
  for (int i = 0; i <= 60; i++) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
  }
  long long t;
  while (cin >> n >> t) {
    n++;
    long long ans = solve(t);
    cout << ans << "\n";
  }
  return 0;
}
