#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30) - 1;
const long double EPS = 1e-9;
const long double PI = fabs(atan2(0.0, -1.0));
int get(int n) {
  int res = 0;
  for (int b = 0; n > 0; b++) {
    if (n & 1) res = b;
    n >>= 1;
  }
  return res;
}
int p[1000100];
void solve() {
  int n;
  cin >> n;
  vector<bool> was(n + 1, false);
  for (int i = n; i >= 0; i--) {
    for (int k = 23; k >= 0; k--) {
      int cur = (1 << k) - 1;
      int add = cur - i;
      if (add < 0 || add > n) continue;
      if (was[add]) continue;
      was[add] = true;
      p[i] = add;
      break;
    }
  }
  printf(
      "%lld"
      "\n",
      n * 1LL * (n + 1));
  for (int i = 0; i <= n; i++) {
    if (i > 0) printf(" ");
    printf("%d", p[i]);
  }
  printf("\n");
}
int main() {
  solve();
  return 0;
}
