#include <bits/stdc++.h>
using namespace std;
long long ans[2][50];
long long ans2[2][50];
int main() {
  long long n, t;
  cin >> n >> t;
  if (t & (t - 1)) {
    printf("%d\n", 0);
    return 0;
  }
  n++;
  int cnt = __builtin_ctzll(t) + 1;
  ans[0][0] = 1;
  for (int i = 60; i >= 0; i--) {
    for (int j = 0; j < 45; j++) {
      ans2[1][j] += ans[1][j];
      ans2[1][j + 1] += ans[1][j];
      if (n & (1LL << i)) {
        ans2[0][j + 1] += ans[0][j];
        ans2[1][j] += ans[0][j];
      } else {
        ans2[0][j] += ans[0][j];
      }
    }
    memcpy(ans, ans2, sizeof(ans));
    memset(ans2, 0, sizeof(ans));
  }
  cout << (ans[0][cnt] + ans[1][cnt]) - (t == 1) << endl;
  return 0;
}
