#include <bits/stdc++.h>
using namespace std;
int N, lim;
int Q[1000010];
bool Used[1000010 * 3];
long long Ans1;
int main() {
  int i;
  scanf("%d", &N);
  for (lim = 0; (1 << lim) <= N; ++lim)
    ;
  cout << (long long)N * (long long)(N + 1) << endl;
  for (i = N; i >= 0; --i) {
    int now = (((1 << lim) - 1) ^ i), maxx = lim - 1, now1 = now;
    for (; now > N || Used[now]; now = ((now - 1) & now1))
      ;
    Used[now] = 1;
    Q[i] = now;
  }
  for (i = 0; i <= N; ++i) printf("%d ", Q[i]);
  puts("");
  return 0;
}
