#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int a[maxn];
int find(int n) {
  if (n < 2) return 0;
  int k;
  for (k = 1; k <= n; k *= 2)
    ;
  return k - 1 - n;
}
int main() {
  int k, n, i, N;
  scanf("%d", &n), N = n;
  while (n >= 0) {
    k = find(n);
    for (i = k; i <= n; ++i) a[i] = n + k - i;
    n = k - 1;
  }
  n = N;
  long long s = 0;
  for (i = 0; i <= n; ++i) s += i ^ a[i];
  printf(
      "%lld"
      "\n",
      s);
  for (i = 0; i <= n; ++i) printf("%d ", a[i]);
  return 0;
}
