#include <bits/stdc++.h>
using namespace std;
int an[1000010];
int main() {
  int i, j, k, n;
  cin >> n;
  k = n;
  while (k > 0) {
    for (i = 1; i < k; i = i * 2 + 1)
      ;
    for (j = k; j >= i - k; j--) an[j] = i - j;
    k = i - k - 1;
  }
  cout << (long long)n * (n + 1) << endl;
  for (i = 0; i <= n; i++) {
    if (i) printf(" ");
    printf("%d", an[i]);
  }
  return 0;
}
