#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-9;
double zero(double d) { return d < EPS ? 0.0 : d; }
int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);
  int n;
  cin >> n;
  bool used[n + 1];
  memset(used, 0, sizeof(used));
  int mx = 0;
  int tmp = n;
  while (tmp > 1) {
    tmp = tmp >> 1;
    mx++;
  }
  int near = pow(2.0, mx + 1) - 1;
  vector<int> res;
  long long sum = 0;
  for (int i = n; i >= 0; i--) {
    while (near - i > n || used[near - i]) {
      if (near == 0) break;
      near = near >> 1;
    }
    int ss = near - i;
    if (i == 0) {
      for (int i = n; i >= 0; i--) {
        if (!used[i]) ss = i;
      }
    }
    res.push_back(ss);
    sum += ss ^ i;
    used[ss] = true;
  }
  cout << sum << endl;
  reverse((res).begin(), (res).end());
  for (int i = 0; i < res.size(); ++i) {
    if (i != 0) cout << " ";
    cout << res[i];
  }
  cout << endl;
  return 0;
}
