#include <bits/stdc++.h>
int n;
long long sum;
int ans[1000050];
int main() {
  scanf("%d", &n);
  int max = n, base = 1;
  while (max > 0) {
    int t_max = max;
    while (t_max >>= 1) base <<= 1;
    for (int i = (base); i <= (max); i++) {
      ans[i] = base * 2 - 1 - i;
      ans[base * 2 - 1 - i] = i;
      sum += 2 * (long long)(i ^ (base * 2 - 1 - i));
    }
    max = 2 * base - 2 - max;
    base = 1;
  }
  printf("%I64d\n", sum);
  for (int i = (0); i <= (n); i++) printf("%d ", ans[i]);
  printf("\n");
}
