#include <bits/stdc++.h>
using namespace std;
long long c[70][70];
long long f(long long n, int b) {
  long long ret = b == 1 ? -1LL : 0LL;
  for (int i = 60; i >= 0; i--)
    if (n & (1LL << i) && b >= 0) {
      ret += c[i][b], b--;
    }
  return ret;
}
int main() {
  long long n, t;
  int b = 0;
  cin >> n >> t;
  c[0][0] = 1LL;
  for (int i = int(1); i <= int(60); i++) c[0][i] = 0LL;
  for (int i = int(1); i <= int(60); i++)
    for (int j = int(0); j <= int(60); j++) {
      if (!j)
        c[i][j] = 1LL;
      else if (j <= i)
        c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
      else
        c[i][j] = 0LL;
    }
  if ((t & (t - 1)))
    puts("0");
  else {
    while (t) t >>= 1, b++;
    cout << f(n + 2LL, b) << endl;
  }
  return 0;
}
