#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, k, L, ans, x;
long long p[55], op[1000005];
bool used[1000005];
long long Getnum(long long x) {
  long long L = 0;
  while (x > 0) {
    L++;
    x >>= 1;
  }
  if (!L) L++;
  return L;
}
int main() {
  scanf("%I64d", &n);
  L = Getnum(n);
  p[1] = 1;
  for (i = 2; i <= L; i++) p[i] = (p[i - 1] << 1) + 1;
  ans = 0;
  for (i = n; i >= 0; i--) {
    if (used[i]) {
      ans += (i ^ op[i]);
      continue;
    }
    L = Getnum(i);
    x = (p[L] ^ i);
    if (used[x]) {
      used[0] = true;
      ans += i;
      op[i] = 0;
      op[0] = i;
    } else {
      used[x] = used[i] = true;
      ans += (x ^ i);
      op[i] = x;
      op[x] = i;
    }
  }
  printf("%I64d\n", ans);
  for (i = 0; i <= n; i++) printf("%I64d ", op[i]);
  printf("\n");
  return 0;
}
