#include <bits/stdc++.h>
using namespace std;
long long n, i, a[2000000], ans, k, mp, lp;
bool fix[2000000];
bool noe;
void anss(int n) {
  if (n == -1) return;
  if (n == 0) return;
  if (n == 1) {
    a[0] = 1;
    return;
  } else if (n == 2) {
    a[0] = 0;
    a[1] = 2;
    a[2] = 1;
    return;
  }
  k = 1;
  while (k <= n) k *= 2;
  k--;
  lp = 0;
  for (i = 0; i <= n; i++) {
    if (!fix[k - i] && k - i >= 0 && k - i <= n) {
      a[i] = k - i;
      fix[k - i] = true;
      lp++;
    }
  }
  anss(n - lp);
}
int main() {
  cin >> n;
  anss(n);
  for (i = 0; i <= n; i++) ans += a[i] ^ i;
  cout << ans << endl;
  for (i = 0; i <= n; i++) cout << a[i] << " ";
  cout << endl;
}
