#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
int n, c[maxn];
long long ans;
void solve(int n) {
  if (n < 0) return;
  if (n == 0) {
    c[0] = 0;
    return;
  }
  int j = 0, m = n;
  while (m > 0) {
    j++;
    m /= 2;
  }
  m = 1 << (j - 1);
  for (int k = 0; k <= n - m; k++) {
    c[m + k] = ((1 << j) - 1) ^ (m + k);
    c[c[m + k]] = m + k;
  }
  solve(m - (n - m + 1) - 1);
}
int main() {
  while (scanf("%d", &n) != EOF) {
    solve(n);
    ans = 0;
    for (int i = 0; i <= n; i++) ans += c[i] ^ i;
    printf("%I64d\n", ans);
    for (int i = 0; i <= n; i++) {
      printf("%d ", c[i]);
    }
    printf("\n");
  }
  return 0;
}
