#include <bits/stdc++.h>
using namespace std;
int n;
const int Maxn = 1e6 + 10;
int a[Maxn];
int main() {
  scanf("%d", &n);
  printf("%I64d\n", 1ll * (n + 1) * n);
  memset(a, -1, sizeof(a));
  for (int i = n; i >= 0; --i) {
    if (a[i] == -1) {
      int tmp = 0;
      for (int j = 0; (1 << (j)) <= i; ++j)
        if (i & (1 << (j)) ^ (1 << (j))) tmp |= (1 << (j));
      a[i] = tmp;
      a[tmp] = i;
    }
  }
  long long check = 0;
  for (int i = 0; i <= n; ++i) {
    check += a[i] ^ i;
    printf("%d%c", a[i], " \n"[i == n]);
  }
  assert(check == 1ll * n * (n + 1));
  return 0;
}
