#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
int aim[maxn], tot = 0;
int n;
int main() {
  memset(aim, 0, sizeof(aim));
  cin >> n;
  int t = 1, m = n;
  while (m > 0) {
    t *= 2;
    m /= 2;
  }
  t--;
  long long ans = 0;
  int pre = n;
  for (int i = n; i >= 0; i--) {
    if (aim[i] != 0) continue;
    int temp = t - i;
    if (temp <= pre) {
      ans += 2 * t;
      aim[i] = temp;
      aim[temp] = i;
      continue;
    }
    t++;
    while ((t / 2 - 1) >= i) {
      t /= 2;
    }
    t--;
    temp = t - i;
    pre = i;
    if (temp <= pre) {
      ans += 2 * t;
      aim[i] = temp;
      aim[temp] = i;
      continue;
    }
  }
  printf("%I64d\n", ans);
  for (int i = 0; i <= n; i++) printf("%d ", aim[i]);
  return 0;
}
