#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, k, m = 0, j, p = 0, pp;
  cin >> n;
  vector<int> a, b(n + 1, 0);
  a.push_back(k = 1);
  while (k <= n) {
    a.push_back(k = k << 1);
    m++;
  }
  for (j = a[m - 1]; j <= n; j++) {
    p = a[m - 1] - (j - a[m - 1] + 1);
    b[j] = p;
    b[p] = j;
  }
  pp = p;
  for (i = m - 2; i >= 0; i--) {
    for (j = a[i]; j < a[i + 1] && j < p; j++) {
      pp = a[i] - (j - a[i] + 1);
      b[j] = pp;
      b[pp] = j;
    }
    p = pp;
  }
  long long kk;
  for (i = 0, kk = 0; i <= n; i++) {
    kk += b[i] ^ i;
  }
  cout << kk << endl << b[0];
  for (i = 1; i <= n; i++) cout << ' ' << b[i];
  cout << endl;
  return 0;
}
