#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ba[n + 1];
  int p = n;
  int ms = n + 1;
  while (p >= 0) {
    int b = 0, t = p, tms = t ^ p, q = 0;
    while (t > 0) {
      t /= 2;
      b++;
    }
    t = (1 << b) - 1;
    tms = t ^ p;
    q = tms;
    while (tms < ms) {
      ba[p--] = tms++;
    }
    ms = q;
  }
  long long t = 0;
  for (int i = 0; i <= n; i++) {
    t += (i ^ ba[i]);
  }
  cout << t << endl;
  for (int i = 0; i <= n; i++) {
    cout << ba[i];
    if (i < n)
      cout << " ";
    else
      cout << endl;
  }
}
