#include <bits/stdc++.h>
using namespace std;
int n, ans[1111111], cur;
bool w[1111111];
long long res;
int main() {
  scanf("%d", &n), memset(w, 0, sizeof(w)), memset(ans, -1, sizeof(ans));
  for (int i = n; i >= 0; --i)
    if (!w[i]) {
      cur = 1;
      while (cur <= i) cur <<= 1;
      --cur, ans[i] = cur - i, w[ans[i]] = w[ans[ans[i]] = i] = true;
    }
  res = 0;
  for (int i = 0; i <= n; ++i) res += i ^ ans[i];
  printf(
      "%lld"
      "\n",
      res);
  for (int i = 0; i <= n; ++i) printf("%d ", ans[i]);
  return 0;
}
