#include <bits/stdc++.h>
using namespace std;
map<long long, long long> A[110];
long long n, t, p;
long long solve(long long n, int p) {
  if (p == -1 || n == 0) return 0;
  if (n == 1) return (p == 0);
  if (A[p].find(n) != A[p].end()) return A[p][n];
  long long put = 1;
  while (put * 2 <= n) put <<= 1;
  if (n == put)
    A[p][n] = (p == 0) + solve(n - 1, p);
  else
    A[p][n] = solve(n - put, p - 1) + solve(put, p);
  return A[p][n];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> t;
  n++;
  while (t % 2 == 0) {
    t /= 2;
    ++p;
  }
  if (t != 1) {
    cout << "0";
    return 0;
  }
  cout << solve(n, p) - (p == 0);
  return 0;
}
