#include <bits/stdc++.h>
using namespace std;
int a[110][110];
long long arr[70];
void see() {
  int m = 35;
  for (int i = 1; i <= m; i++) a[1][i] = 0;
  a[1][m + 1] = 1;
  for (int i = 2; i <= m + 1; i++) {
    for (int j = 1; j <= m + 1; j++) {
      if (j == 1) {
        a[i][j] = a[i - 1][j + 1];
      } else {
        if (j == m + 1) {
          a[i][j] = a[i - 1][j - 1];
        } else {
          a[i][j] = a[i - 1][j - 1] ^ a[i - 1][j + 1];
        }
      }
    }
  }
}
long long pre[50][50], n, m, t;
long long dfs(long long x) {}
int main() {
  cin >> n >> t;
  n++;
  pre[1][1] = 1;
  long long idx = 1, stp = 1;
  while (idx) {
    idx = (idx << 1) | 1;
    if (idx > n) break;
    ++stp;
    for (int i = 40; i >= 2; i--) {
      pre[stp][i] = pre[stp - 1][i] + pre[stp - 1][i - 1];
    }
    pre[stp][1] = pre[stp - 1][1] + 1;
  }
  long long tmp = (long long)log2((long long)t);
  long long ans = 0;
  if ((1LL << (tmp)) == t) {
    long long p = tmp + 1;
    do {
      if (p == 1) {
        if (n) ans += (long long)log2((long long)n) + 1;
        break;
      } else if (stp == 1)
        break;
      long long idx = 1, stp = 1;
      while (idx <= n) {
        idx = (idx << 1) | 1;
        stp++;
      }
      stp--;
      idx = (idx >> 1);
      ans += pre[stp][p];
      if (idx == n) break;
      n = n - idx - 1;
      stp--;
      p--;
    } while (1);
  }
  cout << ans - (t == 1) << endl;
  return 0;
}
