#include <bits/stdc++.h>
using namespace std;
long long k, val;
void rec(int n) {
  if (n <= 0) {
    cout << val << '\n';
    if (!n) cout << "0 ";
    return;
  }
  long long m = 1;
  while ((m << 1) <= n) m <<= 1;
  val += 2 * (n - m + 1) * (2 * m - 1);
  rec(2 * m - n - 2);
  for (int i = n; i >= 2 * m - n - 1; i--) cout << i << ' ';
}
int main() {
  cin >> k;
  rec(k);
  return 0;
}
