#include <bits/stdc++.h>
using namespace std;
const int N = 60;
long long n, t, C[N][N];
long long dfs(long long n, int t) {
  if (n == 0 || t < 0) return 0;
  for (int i = N - 1; i >= 0; --i)
    if ((1ll << i) <= n) return C[i][t] + dfs(n - (1ll << i), t - 1);
  return 0;
}
long long solve(long long n, int x) {
  long long ret = 0;
  for (int i = (0); i < (N); ++i)
    if ((1ll << i) <= n) {
      ret += C[i][x];
      n -= 1ll << i;
    } else {
      ret += dfs(n, x);
      break;
    }
  return ret;
}
int main() {
  for (int i = (0); i < (N); ++i) {
    C[i][0] = C[i][i] = 1;
    for (int j = (1); j < (i); ++j) C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  }
  scanf("%lld%lld", &n, &t);
  if (t == (t & -t)) {
    int x = 0;
    while ((1ll << x) != t) ++x;
    long long ans = solve(n + 1, x) - (x == 0);
    printf("%lld", ans);
  } else {
    printf("0");
  }
  return 0;
}
