#include <bits/stdc++.h>
using namespace std;
int p[1000100];
int main() {
  int i, n, j;
  long long r;
  for (scanf("%d", &n), i = 0; i <= n; p[i] = -1, i++)
    ;
  for (r = 0, i = n; i >= 0; r += i ^ p[i], i--)
    if (p[i] == -1) {
      for (p[i] = 0, j = 0; i >> j; p[i] |= (((i >> j) & 1) ^ 1) << j, j++)
        ;
      p[p[i]] = i;
    }
  printf("%I64d\n", r);
  for (i = 0; i <= n; printf("%d ", p[i]), i++)
    ;
  printf("\n");
  return 0;
}
