#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int aa[N + 1];
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i <= n; i++) aa[i] = i;
  int n_ = n;
  for (int h = 20; n_ > 0;) {
    while (h >= 0 && (n_ & 1 << h) == 0) h--;
    int m = n_ - ((1 << h) - 1);
    reverse(aa + n_ - m * 2 + 1, aa + n_ + 1);
    n_ -= m * 2;
    ans += m * 2LL * ((1 << h + 1) - 1);
  }
  cout << ans << '\n';
  for (int i = 0; i <= n; i++) cout << aa[i] << ' ';
  cout << '\n';
  return 0;
}
