#include <bits/stdc++.h>
using namespace std;
long long n, t;
map<long long, map<int, long long> > mp;
long long gao(long long x, int ns) {
  if (mp[x].find(ns) != mp[x].end()) return mp[x][ns];
  if (ns == 0) return 1;
  if (x == 0) return 0;
  long long lx = x, tx = x;
  while (tx) {
    lx = tx & (-tx);
    tx -= lx;
  }
  return mp[x][ns] = gao(lx - 1, ns) + gao(x - lx, ns - 1);
}
int main() {
  while (cin >> n >> t) {
    if (t == (t & (-t))) {
      int s = 0;
      long long tt = t;
      while (tt) {
        s++;
        tt >>= 1;
      }
      long long ans = gao(n + 1, s);
      if (t == 1) ans--;
      cout << ans << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}
