#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
long N;
long Ans[1000007 + 7];
int main(void) {
  long i, j, a, b, Icase, k = 0;
  scanf("%ld", &N);
  for (i = N; i >= 1; i = j - 1) {
    for (j = i - 1; j >= 0; j--) {
      if (i & j) continue;
      break;
    }
    for (a = j, b = i; a <= i; a++, b--) {
      Ans[a] = b;
    }
  }
  if (i == 1) {
    Ans[1] = 0;
    Ans[0] = 1;
  }
  long long Max = 0;
  for (i = 0; i <= N; i++) {
    Max += i ^ Ans[i];
  }
  printf("%I64d\n", Max);
  for (i = 0; i <= N; i++) {
    if (i) printf(" ");
    printf("%ld", Ans[i]);
  }
  printf("\n");
  return 0;
}
