#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
const int maxn = 1e6 + 5;
long long ans[maxn];
int main() {
  int n;
  cin >> n;
  long long sum = 0, x = 1;
  while (x <= n) {
    x <<= 1;
  }
  x--;
  memset(ans, -1, sizeof(ans));
  for (int i = n; i >= 0; i--) {
    if (ans[i] != -1) {
      continue;
    }
    while ((x ^ i) > n || ans[x ^ i] != -1) {
      x >>= 1;
    }
    ans[x ^ i] = i, ans[i] = x ^ i;
  }
  for (int i = 0; i <= n; i++) {
    sum += i ^ ans[i];
  }
  printf("%I64d\n", sum);
  for (int i = 0; i <= n; i++) {
    printf("%I64d ", ans[i]);
  }
  puts("");
  return 0;
}
