#include <bits/stdc++.h>
using namespace std;
int DEBUG = 0;
int Min(int a, int b) { return (a < b ? a : b); }
int Max(int a, int b) { return (a > b ? a : b); }
int Abs(int a) { return (a >= 0 ? a : -a); }
int64_t num_onebits(int64_t x) {
  int64_t cnt = 0;
  while (x > 0) {
    if (x & 1L) cnt++;
    x = x >> 1;
  }
  return cnt;
}
int64_t num_allbits(int64_t x) {
  int64_t cnt = 0;
  while (x > 0) {
    cnt++;
    x = x >> 1;
  }
  return cnt;
}
int64_t get_bit(int64_t x, int64_t k) {
  while (k > 1) {
    x = x >> 1L;
    k--;
  }
  return x & 1L;
}
int64_t pascal[45][45];
void init_pascal() {
  pascal[0][0] = 1;
  for (int n = 1; n < 45; n++) {
    pascal[n][0] = pascal[n][n] = 1;
    for (int p = 1; p <= n - 1; p++) {
      pascal[n][p] = (pascal[n - 1][p - 1] + pascal[n - 1][p]);
    }
  }
}
int64_t choose(int64_t n, int64_t p) {
  if (n >= 0 && p >= 0 && n >= p) return pascal[n][p];
  return 0;
}
int64_t num_with_t_bits_bf(int64_t n, int64_t t) {
  int64_t cnt = 0;
  for (int64_t i = 0; i < n; i++) {
    if (num_onebits(i) == t) {
      cnt++;
    }
  }
  return cnt;
}
int64_t num_with_t_bits(int64_t n, int64_t t) {
  int64_t cnt = 0;
  int64_t bits_seen = 0;
  for (int64_t pos = num_allbits(n); pos >= 1; pos--) {
    if (get_bit(n, pos) == 1) {
      cnt += choose(pos - 1, t - bits_seen);
      bits_seen++;
    }
  }
  return cnt;
}
int main(int argc, char **argv) {
  DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;
  int64_t n, t;
  cin >> n >> t;
  if (0) {
    int a[2][n + 1];
    int m = n;
    for (int j = 1; j <= m; j++) a[0][j] = 0;
    a[0][m + 1] = 1;
    if (DEBUG) {
      for (int j = 1; j <= m + 1; j++) {
        printf(" %d", a[0][j]);
      }
      printf("\n");
    }
    for (int i = 1; i <= m; i++) {
      int idx0 = (i + 1) % 2;
      int idx = i % 2;
      for (int j = 1; j <= m + 1; j++) {
        if (j == 1)
          a[idx][j] = a[idx0][j + 1];
        else if (j == m + 1)
          a[idx][j] = a[idx0][j - 1];
        else
          a[idx][j] = a[idx0][j - 1] ^ a[idx0][j + 1];
      }
      if (DEBUG) {
        if (1 || i == m + 1) {
          for (int j = 1; j <= m + 1; j++) {
            if (a[idx][j] == 1)
              printf(" %d", a[idx][j]);
            else
              printf("  ");
          }
          printf("\n");
        }
      }
    }
  }
  if (num_onebits(t) != 1) {
    cout << 0 << endl;
    return 0;
  }
  int64_t log2t_plus_1 = num_allbits(t);
  if (DEBUG) {
    cout << "t= " << t << " log2t_plus_1= " << log2t_plus_1 << endl;
  }
  init_pascal();
  int64_t cnt0 = num_with_t_bits(2, log2t_plus_1);
  int64_t cnt = num_with_t_bits(n + 2, log2t_plus_1);
  cnt -= cnt0;
  cout << cnt << endl;
  if (0) {
    log2t_plus_1 = 2;
    for (int64_t i = 0; i <= 10000; i++) {
      int64_t cnt1 = num_with_t_bits_bf(i, log2t_plus_1);
      int64_t cnt2 = num_with_t_bits(i, log2t_plus_1);
      if (num_onebits(i) == 1) {
        cout << "i= " << i << " cnt1= " << cnt1 << " cnt2= " << cnt2 << endl;
      }
    }
  }
  return 0;
}
