#include <bits/stdc++.h>
int a[1000010];
bool f[1000010];
int cal(int x) {
  int i, e, as = 0;
  for (i = 0; (1 << i) <= x; i++) {
    e = x & (1 << i);
    as |= e ? 0 : (1 << i);
  }
  return as;
}
int main() {
  int i, j, n;
  long long sum = 0;
  scanf("%d", &n);
  for (i = n; i >= 0; i--)
    if (!a[i]) {
      j = cal(i);
      a[i] = j;
      a[j] = i;
      sum += (i ^ j) * 2;
    }
  printf("%I64d\n", sum);
  for (i = 0; i <= n; i++) printf("%d ", a[i]);
  return 0;
}
