#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 1000 + 5;
bool used[maxn];
int ans[maxn];
int main() {
  int n;
  scanf("%d", &n);
  long long res = 0;
  int mask = (1 << 22) - 1;
  for (int i = n; i >= 0; --i) {
    if (used[i]) {
      continue;
    }
    while (mask && (mask >> 1) >= i) {
      mask >>= 1;
    }
    int cand = (~i) & mask;
    used[cand] = true;
    ans[i] = cand;
    ans[cand] = i;
    res += (cand ^ i) * 2;
  }
  cout << res << '\n';
  for (int i = 0; i <= n; ++i) {
    printf("%d ", ans[i]);
  }
  putchar('\n');
  return 0;
}
