#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long n;
  cin >> n;
  vector<int> V(n + 1, 0);
  cout << n * (n + 1) << endl;
  while (n > 0) {
    int b = 0;
    int tmp = n;
    while (tmp > 1) {
      tmp >>= 1;
      b++;
    }
    tmp = (1 << b);
    V[tmp] = tmp - 1;
    for (int i = 1; (((tmp - i) >= 0) || (tmp + i <= n)); i++) {
      if ((tmp - i >= 0) && (tmp - 1 + i) <= n) V[tmp - i] = (tmp - 1 + i);
      if (tmp + i <= n) V[tmp + i] = (tmp - 1 - i);
    }
    n = tmp - (n - tmp + 1) - 1;
  }
  for (int i = 0; i < V.size(); i++) {
    cout << V[i] << ' ';
  }
  cout << endl;
  return 0;
}
