#include <bits/stdc++.h>
using namespace std;
long long f[64][128][2];
int a[64];
long long solve(int n, int m, int k, long long num) {
  for (int i = 0; i < n; ++i) a[i] = num & 1, num >>= 1;
  if (a[0] || num > 1) return 0;
  memset(f, 0, sizeof(f));
  f[1][0][0] = 1;
  for (int i = 1; i < m; ++i)
    if (!a[i])
      for (int j = 0; j <= k; ++j) {
        f[i + 1][j][0] += f[i][j][0];
        f[i + 1][j + 1][1] += 2 * f[i][j][1];
        f[i + 1][j + 2][1] += f[i][j][0];
      }
    else
      for (int j = 0; j <= k; ++j) {
        f[i + 1][j][0] += f[i][j][1];
        f[i + 1][j + 1][0] += 2 * f[i][j][0];
        f[i + 1][j + 2][1] += f[i][j][1];
      }
  for (int i = m; i < n; ++i)
    if (!a[i])
      for (int j = 0; j <= k; ++j) {
        f[i + 1][j][0] += f[i][j][0];
        f[i + 1][j + 1][1] += f[i][j][1];
      }
    else
      for (int j = 0; j <= k; ++j) {
        f[i + 1][j][0] += f[i][j][1];
        f[i + 1][j + 1][0] += f[i][j][0];
      }
  return f[n][k][num];
}
int solve_2(int n, int k, long long num) {
  int cnt = 0;
  if (num & 1) return 0;
  num >>= 1;
  for (int i = 1; i < n; ++i) cnt += num & 1, num >>= 1;
  return !num && cnt == k;
}
long long n, ans;
int main() {
  scanf("%lld", &n);
  for (int i = 1; (1ll << i) <= n + 1; ++i) {
    long long l = 1, r = n;
    for (; l <= r;)
      if (((l + r) >> 1) > n / ((1ll << i) - 1))
        r = ((l + r) >> 1) - 1;
      else
        l = ((l + r) >> 1) + 1;
    long long num = n - r * ((1ll << i) - 1);
    for (int k = 0; k < i; ++k) ans += solve_2(i, k, num + k);
  }
  for (int i = 1; (1ll << i) <= n + 1; ++i)
    for (int j = 1; (1ll << j) <= n + 1; ++j) {
      long long l = 1, r = n;
      for (; l <= r;)
        if (2 * ((1ll << i) - 1) + 2 * ((1ll << j) - 1) + 1 >
            (n - (1ll << j) + 1) / ((l + r) >> 1))
          r = ((l + r) >> 1) - 1;
        else
          l = ((l + r) >> 1) + 1;
      if (!r) break;
      long long num =
          n - 2 * r * ((1ll << i) - 1) - (2 * r + 1) * ((1ll << j) - 1) - r;
      for (int k = 0; k < i + j - 1; ++k)
        ans += solve(max(i, j), min(i, j), k, num + k);
    }
  printf("%lld\n", ans);
  return 0;
}
