#include <bits/stdc++.h>
using namespace std;
int a[1000005], f[1000005], x, k, i, n;
long long ans = 0;
int main() {
  scanf("%d", &n);
  for (i = n; i; i--)
    if (!f[i]) {
      k = (int)(log2(i)) + 1;
      x = (1 << k) - 1 - i;
      f[x] = 1;
      a[i] = x;
      a[x] = i;
      ans += (long long)((1 << k) - 1) << 1;
    }
  printf("%I64d\n", ans);
  for (i = 0; i <= n; i++) printf("%d ", a[i]);
}
