#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000001;
const int K = 20;
int zcs[K], ocs[K];
int ps[MAX_N];
int main() {
  int n;
  scanf("%d", &n);
  n++;
  long long m = 0;
  for (int k = 0, b0 = 1; k < K; k++, b0 <<= 1) {
    int b1 = b0 << 1;
    int d = n / b1, r = n % b1;
    zcs[k] = ocs[k] = d * b0;
    if (r < b0)
      zcs[k] += r;
    else
      zcs[k] += b0, ocs[k] += r - b0;
    m += (long long)b0 * min(zcs[k], ocs[k]);
  }
  m *= 2;
  for (int a = n - 1; a >= 0; a--) {
    int p = 0;
    for (int k = 0, bk = 1; k < K; k++, bk <<= 1) {
      if (!(a & bk)) {
        if (ocs[k] > 0)
          p |= bk, ocs[k]--;
        else
          zcs[k]--;
      } else {
        if (zcs[k] > 0)
          zcs[k]--;
        else
          p |= bk, ocs[k]--;
      }
    }
    ps[a] = p;
  }
  printf("%lld\n", m);
  for (int i = 0; i < n; i++) {
    if (i) putchar(' ');
    printf("%d", ps[i]);
  }
  putchar('\n');
  return 0;
}
