#include <bits/stdc++.h>
using namespace std;
struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};
long long n_, n;
int ans[1000010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n;
  n_ = n;
  printf("%I64d\n", n * (n + 1));
  while (n >= 0) {
    int N = 0;
    while ((1 << N) - 1 < n) N++;
    int from = (1 << N) - 1 - n;
    for (int i = from; i < (n + 1); i++) {
      ans[i] = (1 << N) - 1 - i;
    }
    n = from - 1;
  }
  for (int i = 0; i < (n_ + 1); i++) printf("%d ", ans[i]);
}
