#include <bits/stdc++.h>
using namespace std;
int p[1000006];
long long int ans;
void solve(int n) {
  if (n <= 0) return;
  int k = 1;
  while (k <= n) k <<= 1;
  k >>= 1;
  int dec = k - 1;
  for (int i = (int)(k); i < (int)(n + 1); i++) {
    p[i] = dec;
    p[dec] = i;
    dec--;
  }
  solve(dec);
}
int main() {
  int n;
  scanf("%d", &(n));
  solve(n);
  for (int i = (int)(0); i < (int)(n + 1); i++) ans += i ^ p[i];
  cout << (ans) << endl;
  for (int i = (int)(0); i < (int)(n + 1); i++) {
    if (i) printf(" ");
    printf("%d", p[i]);
  }
  puts("");
  return 0;
}
