#include <bits/stdc++.h>
using namespace std;
long long pow(long long b, long long e, long long m) {
  long long t = 1;
  for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0;
  return t;
}
template <class T>
inline bool chkmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T>
inline bool chkmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
T gcd(T x, T y) {
  for (T t; x; t = x, x = y % x, y = t)
    ;
  return y;
}
template <class edge>
struct Graph {
  vector<vector<edge> > adj;
  Graph(int n) {
    adj.clear();
    adj.resize(n + 5);
  }
  Graph() { adj.clear(); }
  void resize(int n) { adj.resize(n + 5); }
  void add(int s, edge e) { adj[s].push_back(e); }
  void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
  vector<edge> &operator[](int t) { return adj[t]; }
};
long long cb[60][60];
int main() {
  ios_base::sync_with_stdio(false);
  long long n, t;
  cin >> n >> t;
  if (t & (t - 1)) return cout << 0 << endl, 0;
  int e = 0;
  for (; t != 1; t >>= 1, ++e)
    ;
  for (int i = 0; i < 60; ++i)
    for (int j = 0; j <= i; ++j)
      cb[i][j] = j ? cb[i - 1][j - 1] + cb[i - 1][j] : 1;
  long long ans = 0;
  int x = 0;
  n += 2;
  ({});
  ++e;
  for (int b = 60 - 1; b >= 0; --b) {
    if (n >> b & 1) {
      cerr << b << " " << e - x << " " << cb[b][e - x] << endl;
      ans += cb[b][e - x];
      ++x;
      if (x > e) break;
    }
  }
  if (e == 1) ans -= 1;
  cout << ans << endl;
  return 0;
}
