#include <bits/stdc++.h>
using namespace std;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1};
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const long long INFLL = 0x7FFFFFFFFFFFFFFFLL;
const double pi = acos(-1);
template <class T>
T take(queue<T> &O) {
  T tmp = O.front();
  O.pop();
  return tmp;
}
template <class T>
T take(stack<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <class T>
T take(priority_queue<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <class T>
inline void getint(T &num) {
  bool neg = 0;
  num = 0;
  char c;
  while ((c = getchar()) && (!isdigit(c) && c != '-'))
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  do num = num * 10 + c - '0';
  while ((c = getchar()) && isdigit(c));
  num *= (neg) ? -1 : 1;
}
void OPEN(string in = "input.txt", string out = "output.txt") {
  freopen(in.c_str(), "r", stdin);
  freopen(out.c_str(), "w", stdout);
  return;
}
long long dp[66][105][2];
long long n, t, bc;
long long f(int pos, int kleft, int under) {
  if (pos == -1) return (kleft == 0);
  long long &ret = dp[pos][kleft][under];
  if (ret != -1) return ret;
  ret = 0;
  if (kleft) {
    if (under || (n >> pos) & 1) ret += f(pos - 1, kleft - 1, under);
  }
  ret += f(pos - 1, kleft, under | ((n >> pos) & 1));
  return ret;
}
int main() {
  getint(n);
  getint(t);
  if ((t & -t) != t) {
    puts("0");
    return 0;
  }
  bc = __builtin_ctzll(t) + 1;
  memset(dp, -1, sizeof(dp));
  ++n;
  printf("%I64d\n", f(63 - __builtin_clzll(n), bc, 0) - (t == 1LL ? 1 : 0));
  return 0;
}
