#include <bits/stdc++.h>
using namespace std;
long long dp[60][60];
long long ans = 0;
vector<int> toBinary(long long n) {
  vector<int> ans;
  while (n) {
    ans.push_back(n % 2);
    n /= 2;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
long long nCr(long long n, long long r) {
  if (r > n || r < 0) return 0;
  if (n == r || r == 0) return 1;
  if (dp[n][r] != 0) return dp[n][r];
  dp[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r);
  return dp[n][r];
}
int main() {
  long long n, t;
  cin >> n >> t;
  ++n;
  int req = -1;
  long long pro = 1;
  long long cnt = 0;
  while (pro <= t) {
    if (pro == t) {
      req = cnt;
      break;
    }
    pro *= 2ll;
    ++cnt;
  }
  if (req == -1) {
    cout << 0 << endl;
    return 0;
  }
  req++;
  long long ans = 0;
  long long curr = 0;
  vector<int> bf = toBinary(n);
  for (int i = 0; i < bf.size(); ++i) {
    if (bf[i] == 1) {
      ans += nCr(bf.size() - i - 1, req - curr);
      ++curr;
    }
  }
  if (curr == req) ++ans;
  cout << ans - (t == 1) << endl;
}
