#include <bits/stdc++.h>
using namespace std;
map<long long, long long> dp;
long long ff(long long n) {
  int x = 64 - __builtin_clzll(n) - 1;
  return (1ll << x) ^ n;
}
long long f(long long n) {
  if (n == 0) return 0;
  if (n == 1) return 1;
  if (n == 2) return 3;
  if (dp[n] != 0) return dp[n];
  long long ans = 0;
  for (long long p = 1; p <= n; p *= 2) {
    long long q = min(2 * p - 1, n);
    ans += p + f(ff(q));
  }
  return dp[n] = ans;
}
int main() {
  long long n;
  cin >> n;
  cout << f(n - 1) << endl;
  return 0;
}
