#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const double eps = 1e-8;
void read(int &k) {
  k = 0;
  char x = getchar();
  while (x < '0' || x > '9') x = getchar();
  while (x >= '0' && x <= '9') {
    k = k * 10 - 48 + x;
    x = getchar();
  }
}
long long tot;
int n, N, x, o, ans[1000010];
int main() {
  scanf("%d", &n);
  N = n;
  while (n > 0) {
    for (x = 0;; x++)
      if ((1 << x) > n) break;
    x--;
    o = 1 << x;
    for (int i = o; i <= n; i++) {
      ans[i] = o - (i - o) - 1;
      ans[o - (i - o) - 1] = i;
      tot += 2 * (i ^ (o - (i - o) - 1));
    }
    n -= 2 * (n - o + 1);
  }
  printf("%I64d\n", tot);
  for (int i = 0; i <= N; i++) printf("%d ", ans[i]);
  return 0;
}
