#include <bits/stdc++.h>
using namespace std;
long long n;
long long pot[32];
int main() {
  pot[0] = 1;
  for (int i = 1; i < 32; i++) pot[i] = pot[i - 1] * 3LL - 1LL;
  cin >> n;
  long long sum = 0, cfact = 1LL, magija = 0LL, rj = 0LL;
  for (int i = 31; i >= 0; i--) {
    cfact += magija;
    if (sum + (1LL << i) <= n) {
      sum += (1LL << i);
      rj += cfact * pot[i] - magija;
      magija++;
      cfact += magija;
    }
  }
  cout << rj;
}
